2012-04-26 50 views
3

我需要我一點幫助。我需要使用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); 

}}

+1

你有沒有[檢查文檔](http://developer.android.com/reference/android/os/AsyncTask.html)? – dmon 2012-04-26 14:45:35

+1

請發佈您的logcat中的任何錯誤,並告訴我們您的具體問題是什麼。 – Sam 2012-04-26 14:58:44

+0

我建議你檢查文檔。和[這個例子](http://www.technotalkative.com/android-google-image-search-api-example-json-parsing-web-api-call-demo/) – 2012-04-27 06:54:55

回答

8

試試這個

new MyAsyncTask.execute("http://10.10.10.10/data.php"); 

聲明的任務,因爲

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); 

    } 

希望它有幫助。

+0

沒有。這麼多sintax錯誤:( – Nenad 2012-04-27 07:25:40

+0

好吧,我已經做了一些編輯,再試一次,它應該工作 – Som 2012-04-27 07:49:50

+0

你能向我解釋什麼是YourActivity.this?謝謝 – Nenad 2012-04-27 13:43:00

2

不要在您的onCreate()下載任何數據 - 如果時間過長,那麼你會得到ANR異常(活動不響應)。你應該在你的問題中使用AsyncTask。對於的AsyncTask您對Android的網站很好的例子:

http://developer.android.com/reference/android/os/AsyncTask.html

你應該把JSONfunctions.getJSONfromURL()內的doInBackground()

和所有onPostExecute()下面什麼