notifyDataSetChanged example

后端 未结 5 924
再見小時候
再見小時候 2020-11-21 07:28

I\'m trying to use in my Android Application the notifyDataSetChanged() method for an ArrayAdapter but it doesn\'t work for me.

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-21 08:05

    I know this is a late response but I was facing a similar issue and I managed to solve it by using notifyDataSetChanged() in the right place.

    So my situation was as follows.

    I had to update a listview in an action bar tab (fragment) with contents returned from a completely different activity. Initially however, the listview would not reflect any changes. However, when I clicked another tab and then returned to the desired tab,the listview would be updated with the correct content from the other activity. So to solve this I used notifyDataSetChanged() of the action bar adapter in the code of the activity which had to return the data.

    This is the code snippet which I used in the activity.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    
        switch (item.getItemId()) 
        {
            case R.id.action_new_forward:
    
                FragmentTab2.mListAdapter.notifyDataSetChanged();//this updates the adapter in my action bar tab
                Intent ina = new Intent(getApplicationContext(), MainActivity.class);
                ina.putExtra("stra", values1);
                startActivity(ina);// This is the code to start the parent activity of my action bar tab(fragment).
        }
    }
    

    This activity would return some data to FragmentTab2 and it would directly update my listview in FragmentTab2.

    Hope someone finds this useful!

提交回复
热议问题