Search code examples
androidlistviewbaseadapternotifydatasetchanged

android notifyDataSetChanged not working if delete item from data item


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.


Solution

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