Updating ListView by notifydataSetChanged, has to use runOnUiThread?

依然范特西╮ 提交于 2019-12-11 03:52:41

问题


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.
        }
    }

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?

(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()

EDIT:

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

and in my onActivityCreated:

new addViewsToList().execute();

and on my 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 :)


回答1:


Indeed, you have to make Runnable.

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.



来源:https://stackoverflow.com/questions/6837397/updating-listview-by-notifydatasetchanged-has-to-use-runonuithread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!