2010-09-28 44 views
0

我想動態加載一個ListView,例如,在滾動期間加載它們,所以它不會加載我所有的100個帖子。我怎樣才能做到這一點?動態加載ListView

我在這裏看過類似的解決方案,但由於我沒有得到它的工作,我問了這個問題。

我的代碼:

//orders is my is my ArrayList<WithMyOwnOrderClass> that handling orders 
/* adapter is my inner private class in this piece of code, that trigging 
the getView */ 
if(orders != null && orders.size() > 0){ 

for(int i=0;i<orders.size();i++){   
    adapter.add(orders.get(i)); 
} 
      adapter.notifyDataSetChanged(); 
      progressDialog.dismiss(); 
} 

我在同一個java文件,功能,通過100個項目從網絡下載和循環的信息,像這樣:

JSONObject jObject = new JSONObject(queryResult); 
SONArray docsArray = jObject.getJSONArray("array"); 
    for (int i = 0; i < 100; i++) { 
     try {  
      title = docsArray.getJSONObject(i).optString("title"); 
      } catch (JSONException e) {}  
    } 

,然後將其正確添加新訂單等等。 但現在!(?)我想這樣做,當第一個項目加載時,它應該出現,並在滾動時逐漸加載。

希望這可以實現,並提前感謝任何幫助!

回答

3

我打算假設您從單個Web服務調用中獲得項目的JSON。

如果是這樣,請在AsyncTask中執行您的查詢和JSON解析。您的實施doInBackground()與每個項目呼叫publishProgress()。將onProgressUpdate()的執行號碼add()ArrayAdapter(在ArrayList<>上)附加到ListView。演示該技術的Here is a sample project

+0

謝謝!現在就執行它! – Curtain 2010-09-29 06:05:13