异步任务完成后更新列表视图

发布于 2025-01-07 04:57:07 字数 1378 浏览 1 评论 0原文

异步任务完成后如何更新 listivew。以下是示例代码,但列表视图未更新。

    class CallXML extends AsyncTask<Void, Void, Void> {
    int gcid;
    int scid;

    public CallXML(int gid, int sid) {
        // TODO Auto-generated constructor stub
        gcid = gid;
        scid = sid;
    }

    protected void onPreExecute() {

    }

    protected Void doInBackground(Void... arg0) {
        // here goes the xml parsing....
        }   

        return null;            
    }

    protected void onPostExecute(String result) {  
       Log.e("TAG", "In postExecute");


       Cursor cur3 = database2.query("Quote", qfield,  null, null, null, null, null);
       cur3.moveToFirst();

       do {
        quotesarray.add(cur3.getString(2));

       }  while (cur3.moveToNext());

        if(cur3 != null){
            cur3.close();
        }

        QList.post(new Runnable() {
             public void run() {
                mAdapter = new CustomAdapter();
                mAdapter.notifyDataSetChanged();
                QList.setAdapter(mAdapter);    
             }
         });


        if (helper2 != null) {
            helper2.close();
        }

        if (database2 != null) {
            database2.close();
        } 

    }
}

编辑:

实际上 onPostExecute 没有执行,为什么..这是我调用 asynctask new CallXML(gcid, scid).execute(); 的方式

How do i update listivew when async task is done. Below is the sample code but the listview isn't updated.

    class CallXML extends AsyncTask<Void, Void, Void> {
    int gcid;
    int scid;

    public CallXML(int gid, int sid) {
        // TODO Auto-generated constructor stub
        gcid = gid;
        scid = sid;
    }

    protected void onPreExecute() {

    }

    protected Void doInBackground(Void... arg0) {
        // here goes the xml parsing....
        }   

        return null;            
    }

    protected void onPostExecute(String result) {  
       Log.e("TAG", "In postExecute");


       Cursor cur3 = database2.query("Quote", qfield,  null, null, null, null, null);
       cur3.moveToFirst();

       do {
        quotesarray.add(cur3.getString(2));

       }  while (cur3.moveToNext());

        if(cur3 != null){
            cur3.close();
        }

        QList.post(new Runnable() {
             public void run() {
                mAdapter = new CustomAdapter();
                mAdapter.notifyDataSetChanged();
                QList.setAdapter(mAdapter);    
             }
         });


        if (helper2 != null) {
            helper2.close();
        }

        if (database2 != null) {
            database2.close();
        } 

    }
}

EDIT:

Acutally onPostExecute is not executed why..This is the way I call asynctask new CallXML(gcid, scid).execute();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹤仙姿 2025-01-14 04:57:07

此外,onPostExecute 位于主线程上,因此不应在那里进行数据库查询。相反,在 doInBackground 中获取数据并从那里返回最终集合。

onPostExecute 可用于 UI 更新并通过结果集合更新适配器。

编辑:

 QList.post(new Runnable() {
                 public void run() {
                    //mAdapter.notifyDataSetChanged();
                    QList.setAdapter(mAdapter);    
                 }
             });

不需要发布可运行的文件,因为您处于主循环中。

Also, onPostExecute is on the main thread so should not be doing database queries there. Instead, get data in doInBackground and return the final collection from there.

onPostExecute can be used for UI updates and updating your adapter with result collection.

Edit: posting a runnable

 QList.post(new Runnable() {
                 public void run() {
                    //mAdapter.notifyDataSetChanged();
                    QList.setAdapter(mAdapter);    
                 }
             });

is not required since you are in the main loop.

饮湿 2025-01-14 04:57:07

您没有在代码中向适配器提供字符串项。当你将适配器设置为列表时,你不需要调用notifyDataSetChanged,因为当你设置适配器时,它会自动将数据加载到列表中。也许你我尝试这样做:

protected void onPostExecute(String result) {  
       Log.e("TAG", "In postExecute");

       Cursor cur3 = database2.query("Quote", qfield,  null, null, null, null, null);
       cur3.moveToFirst();

       mAdapter = new CustomAdapter();

       do {
        mAdapter.add(cur3.getString(2));

       }  while (cur3.moveToNext());

        if(cur3 != null){
            cur3.close();
        }

        QList.post(new Runnable() {
             public void run() {
                //mAdapter.notifyDataSetChanged();
                QList.setAdapter(mAdapter);    
             }
         });


        if (helper2 != null) {
            helper2.close();
        }

        if (database2 != null) {
            database2.close();
        } 

    }

You are not providing string items to the adapter in your code. And you don't need to call notifyDataSetChanged when you are setting adapter to a list, because when you set an adapter, it automatically loads the data into list. Perhaps you me try doing it in this way:

protected void onPostExecute(String result) {  
       Log.e("TAG", "In postExecute");

       Cursor cur3 = database2.query("Quote", qfield,  null, null, null, null, null);
       cur3.moveToFirst();

       mAdapter = new CustomAdapter();

       do {
        mAdapter.add(cur3.getString(2));

       }  while (cur3.moveToNext());

        if(cur3 != null){
            cur3.close();
        }

        QList.post(new Runnable() {
             public void run() {
                //mAdapter.notifyDataSetChanged();
                QList.setAdapter(mAdapter);    
             }
         });


        if (helper2 != null) {
            helper2.close();
        }

        if (database2 != null) {
            database2.close();
        } 

    }
微暖i 2025-01-14 04:57:07

你有什么错误吗?如果是这样,请发布。如果没有检查从数据库获取的数据大小,如果您想刷新列表视图,只需调用 listview.invalidateViews() 它将刷新列表视图并在列表视图中设置新数据。

Did u got any error? if so please post it. If not Check the size of data you got from database and if you want to refresh listview just call listview.invalidateViews() it will refresh the list view and set the new data in the listview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文