本文介绍了使用的AsyncTask来显示的ListView数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要我的一点点帮助。我需要在ListView中使用的AsyncTask 以显示数据。但我不知道该怎么becouse我是新的机器人编程......非常感谢你的帮助。
I need me a little help. I need to use asynctask to display data in ListView. But I don't know how becouse I'm new in Android programming ... thank you very much for any help.
public class Main extends ListActivity {
Button buttonbg;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://10.10.10.10/data.php");
try{
JSONArray ip = json.getJSONArray("ip");
for(int i=0;i<ip.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = ip.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("data1", e.getString("date"));
map.put("data2", "Location:" + e.getString("location") + " Status:" + e.getString("status"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "data1", "data2" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
}}
推荐答案
试试这个
new MyAsyncTask.execute("http://10.10.10.10/data.php");
声明的任务,因为
Declare the task as
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > {
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
JSONObject json = JSONfunctions.getJSONfromURL(params[0]);
try {
JSONArray ip = json.getJSONArray("ip");
for (int i=0;i<ip.length();i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = ip.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("data1", e.getString("date"));
map.put("data2", "Location:" + e.getString("location") + " Status:" + e.getString("status"));
mylist.add(map);
}
return mylist
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
ListAdapter adapter = new SimpleAdapter(YourActivity.this, result , R.layout.main,
new String[] { "data1", "data2" },
new int[] { R.id.item_title, R.id.item_subtitle });
YourActivity.this.setListAdapter(adapter); // If Activity extends ListActivity
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
}
希望它帮助。
Hope it helps.
这篇关于使用的AsyncTask来显示的ListView数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!