android notifyDataSetChanged not working if delete item from data item

3.3k views Asked by At

I am developing an android application in which I am using one base adapter and displaying data in list form.Now what happened If I remove any object from list and if I call notifyDataSetChanged, its not working as expected. I tried this in following manner.

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);



    shareDataList = getData();
    shareListAdapter = new SharedHistoryListAdapter(this, shareDataList);
    sharedList.setAdapter(shareListAdapter);

}

@Override
protected void onResume()
{

     super.onResume();

    // after deleting data it is coming inside onresume ... 

    shareDataList = getData();

    shareListAdapter.notifyDataSetChanged();

}

I checked data size inside my activity and inside adapter as well. So in activity it is showing count as expected but inside adapter it is not showing updated count. Am I missing something? Need some help.

2

There are 2 answers

3
Kartheek On BEST ANSWER

You cannot change the reference of the list after setting the list to the adapter. so remove the line

 shareDataList = getData(); 

and replace with

    shareDataList.clear();
    shareDataList.addAll(getData());

and now invoke the shareListAdapter.notifyDataSetChanged();

0
Krishna V On

I think, in getData() method gets every time new ArrayList object. You should simply call

shareListAdapter.notifyDataSetChanged();

it will work perfectly