2014-07-02 84 views
0

我希望每一個新項目時更新我的​​列表視圖進度對話框中添加到我的ArrayList中顯示出這是我的代碼如何更新列表視圖項目的每個項目添加

public class Load extends AsyncTask<Void, Void, Void> { 

    ProgressDialog progress; 

    @Override 
    protected void onPreExecute() { 
     progress = new ProgressDialog(SearchList.this); 
     progress.setMessage("loading...."); 
     progress.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 
     // do tracks loading process here, don't update UI directly here 
     // because there is different mechanism for it 

     //FlowableBookViewer.webview.loadUrl("javascript:(function(){var txt = window.getSelection();window.name= txt;window.cpjs.sendToAndroid(window.name);})()"); 

     for (int i = 0; i < Globals.currenHtmlList.size(); i++) { 
      try { 
       String pageText = FunctionsClass 
         .readTextFromHtml(Globals.currenHtmlList.get(i)); 
       if (pageText.toLowerCase().contains(
         Globals.SelectedText.toLowerCase())) { 
        String pagename = new File(
          Globals.currenHtmlList.get(i)).getName(); 
        SearchItem sitem = new SearchItem(); 
        sitem.setTargetList(Globals.currenHtmlList.get(i)); 
        sitem.setPageNumber(i); 
        if (pagename.endsWith("html")) { 
         pagename = pagename.substring(0, 
           pagename.length() - 5); 
        } else if (pagename.endsWith("htm")) { 
         pagename = pagename.substring(0, 
           pagename.length() - 4); 
        } else if (pagename.endsWith("xhtml")) { 
         pagename = pagename.substring(0, 
           pagename.length() - 6); 
        } 

        sitem.setTitleList("Page " + pagename); 
        founded.add(sitem); 





       } 

      } catch (FileNotFoundException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     // write display tracks logic here 
     progress.dismiss(); // dismiss dialog 
     m_adapter = new OrderAdapter(SearchList.this, R.layout.itemview, 
       founded); 
     lv.setAdapter(m_adapter); 
     lv.setTextFilterEnabled(true); 
    } 
} 

在此代碼列表視圖項後完全出現for循環將sitem對象的所有項添加到創建的列表我想更新listview添加到創建的每個項目。 非常感謝

+0

首先在添加每個項目後刷新列表並不好。您可以在'doInBackground'中運行使用UI線程並調用'm_adapter.notifyDatasetChanged();',但是您需要在此之前創建適配器並創建類的實例類 –

+0

添加新項目後,只需通知您的列表適配器。 –

回答

0

1)創建適配器調用AsyncTask

移動這對你的活動/片段onCreate方法之前:

m_adapter = new OrderAdapter(SearchList.this, R.layout.itemview, 
        founded); 
lv.setAdapter(m_adapter); 
    lv.setTextFilterEnabled(true); 

2)你以後在你的ArrayList調用添加項onProgressUpdateAsyncTask

在你doInBackground添加方法:

founded.add(sitem); 
publishProgress(); 

3)在onProgressUpdate方法調用notifyDatasetChanged方法從適配器

@Override 
protected void onProgressUpdate(Void... v) { 
    super.onProgressUpdate(v); 
    m_adapter.notifyDataSetChanged(); 
} 

你可以閱讀更多關於notifyDataSetChangedhere

+0

你可以請注意我簡單的代碼到你的想法,因爲我嘗試過但沒有任何改變。謝謝你的幫助 – Ayman

+0

@ user3764397我已經更新了我的答案 – LordRaydenMK

+0

感謝您的幫助,但是爲什麼在完成搜索後它沒有隱藏進度對話框,並且它運行了一個異常,儘管我編輯了onPostExecute這個progress.dismiss(); – Ayman

相關問題