如何更新列表视图项目添加的每个项目

时间:2014-07-02 09:30:05

标签: android android-listview

我希望在进度对话框中添加到我的arraylist的每个新项目期间更新我的listview,显示这是我的代码

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

在此代码中,listview项目在完成for循环后出现,它将sitem对象的所有项目添加到创建列表中我想在添加到established的每个项目中更新listview。 非常感谢

1 个答案:

答案 0 :(得分:0)

1)在调用AsyncTask

之前创建适配器

将其移至您的活动/片段onCreate方法:

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

2)在ArrayList中添加项目后,从onProgressUpdate

调用AsyncTask

doInBackground方法中添加:

founded.add(sitem);
publishProgress();

3)在onProgressUpdate方法中,从适配器调用notifyDatasetChanged方法

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

您可以详细了解notifyDataSetChanged here