我需要我一點幫助。我需要使用asynctask在ListView中顯示數據。但我不知道我在Android編程方面有什麼新感覺......非常感謝您的幫助。使用AsyncTask在ListView中顯示數據
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);
}}
你有沒有[檢查文檔](http://developer.android.com/reference/android/os/AsyncTask.html)? – dmon 2012-04-26 14:45:35
請發佈您的logcat中的任何錯誤,並告訴我們您的具體問題是什麼。 – Sam 2012-04-26 14:58:44
我建議你檢查文檔。和[這個例子](http://www.technotalkative.com/android-google-image-search-api-example-json-parsing-web-api-call-demo/) – 2012-04-27 06:54:55