通过notifydataSetChanged更新ListView中,必须使用runOnUiThread? [英] Updating ListView by notifydataSetChanged, has to use runOnUiThread?

查看:918
本文介绍了通过notifydataSetChanged更新ListView中,必须使用runOnUiThread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的ListView,使用自己的定制arrayadapter。它的工作原理从数据库等大,中获取数据,但其集之后就没有更新了。我有这样的活动,做东西的结果,当它回来我称之为:

So i have this ListView, that uses my own custom arrayadapter. It works great, getting data from the database etc, but after its set there is no updating it. I have this activity that does something for result and when it comes back I call this:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);          
        if(requestCode==1899)
        {       
            //a new client was added to the list...
            try {
                clients = (ArrayList<ClientsData>) clientsDataDao.queryForAll();
            } catch (SQLException e) {
                // TODO Auto-generated catch block

            }                           
            theClients.notifyDataSetChanged(); //this is not working here                           
            showClientDetails(mCurrentSelectedItemIndex); //update other fragment.
        }
    }

问题是, notifyDataSetChanged()实际上并没有做任何事情。我没有进一步的调查,我想这对UI线程被称为?但我在FragmentActivity,我不知道如何做到这一点?我可能要做出一个运行线程,并从那里打电话了吗?没有做太多有这么不确定线程​​,任何一个有关于如何做到这一点很好的(简洁和完整)的例子吗?或者这是连我的问题呢?

The issue is that the notifyDataSetChanged() doesn't actually do anything. I did investigate further and I guess it has to be called on the UI Thread? but I am in a FragmentActivity and I am not sure how to do this? I might have to make a runnable thread and call it from there? Haven't done much with threads so not sure, any one have good (concise and complete) examples on how to do this? or is this even my problem?

(注:我切换到ORM精简版,这个我在这个特殊的点之前, SimpleCursorAdapter ,我可以打电话后更新 .requery 那么 notifyDataSetChanged()

(Note: I switched to ORM Lite, before this i had at this particular spot a SimpleCursorAdapter that I could update after calling a .requery on my cursor and then the notifyDataSetChanged()

编辑:

好了,经过几次意见,我调查了AsyncTask的路线......

Okay so after a few comments I investigated the AsyncTask route...:

private class addViewsToList extends AsyncTask<Void, Void, Boolean>{
         protected void onPostExecute(Boolean result) {
                theClients.notifyDataSetChanged();
            }
        @Override
        protected Boolean doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return true;
        }         
    }

在我的 onActivityCreated

new addViewsToList().execute();

和我的的onActivityResult(...)

super.onActivityResult(requestCode, resultCode, data);          
        if(requestCode==1899)
        {       
            //a new client was added to the list...
            try {
                clients = (ArrayList<ClientsData>) clientsDataDao.queryForAll();
            } catch (SQLException e) {
                // TODO Auto-generated catch block

            }       
                            //do I have to reset the adapter?   
            setListAdapter(theClients);             
                        showClientDetails(mCurrentSelectedItemIndex);
        }

仍然没有更新列表...

Still no update to the list...

所以,我证明了我不知道我在做什么:)

So I demonstrated I have no clue what I am doing :)

推荐答案

事实上,你必须做出Runnable接口。

Indeed, you have to make Runnable.

如何? REFEED在那里你更新新的数据适配器(我假设你想改变的东西在ListView),然后调用 notifyDataSetChanged()在运行的。事实上,你需要 runOnUIThread(),因为你是触发用户界面的更新。或者使用的AsyncTask 在这里你更新列表视图无论是在 onPostExecute() onProgessUpdate()方法。

How? Refeed the adapter where you update the new data (I assume you want to change something in the listview) and then call notifyDataSetChanged() in the runnable. Indeed you need runOnUIThread() because you are triggering updates on the ui. Or use AsyncTask where you update the listview either in onPostExecute() or onProgessUpdate() methods.

这篇关于通过notifydataSetChanged更新ListView中,必须使用runOnUiThread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆