开发者

Updating ListView by notifydataSetChanged, has to use runOnUiThread?

开发者 https://www.devze.com 2023-03-22 22:19 出处:网络
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

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号