0

I have three fragments that contains three ListView created using a BaseAdapter. Like the image below:

enter image description here

Anyway my listView is fetching data from an SqliteDatabase. What I need to know is: when I delete an item from my ListView My(Favorite,Rejected) Fragments ListViews are not notified and are not refreshing.

What I have tried so far is :

Call listView.invalidateViews() after notifyDataSetChanged() in the onResume() Method of my fragments .

I tried these solution two Android ListView not refreshing after notifyDataSetChanged

My code is : In My BaseAdapter I'am using these method to refresh my adapter :

   public void UpdateView(List<Voiture> items) 
   {
    this.voitureList = items;
    notifyDataSetChanged();
     }

In My fragments I'am using these method to notify the adapter :

 @Override
public void onResume() {
    super.onResume();
    adapterLogin.UpdateView(databaseHelper.getAllVoiture(username,currentLength));
    listView.setAdapter(new     AdapterLogin(getActivity(),voitureList,username,currentLength,1));

}

In the OncreateView() Method I'am using :

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    inflate = inflater ;
    x = inflater.inflate(R.layout.fragemnt_favoris,null);
    empty = (TextView) x.findViewById(R.id.texteempty);
    listView = (ListView) x.findViewById(R.id.list);

    activity = (Main2Activity) getActivity() ;
    username = activity.getUsername();
    databaseHelper = new DatabaseHelper(getActivity());
    databaseHelper.InsertActivity(2,username);
    voitureList = databaseHelper.getAllVoitureFavourite(1,username);
    adapterLogin = new AdapterLogin(getActivity(),voitureList,username,currentLength,2);
    if (voitureList.size()>0)
    {
        listView.setAdapter(adapterLogin);
        ((BaseAdapter)listView.getAdapter()).notifyDataSetChanged();


    }
    else
    {
        empty.setVisibility(View.VISIBLE);

    }



    // Inflate the layout for this fragment
    return  x;


}

Any help would be greatly appreciated.

Community
  • 1
  • 1
Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33
  • Why don't you just use a Event Bus library for notifying different parts of your app with events? Such libs are: Otto, EventBus, RxBus-Android... :) – Seishin Aug 17 '16 at 08:23
  • 1
    Hi Ahlem, have you tried using notifyDataSetInvaldated() instead? source: https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetInvalidated() – Paul Aug 17 '16 at 08:24
  • @Paul : Yes i used notifyDataSetInvalda‌​ted() but it didn't work for me ! – Ahlem Jarrar Aug 17 '16 at 08:28
  • @Seishin : i didn't think about events perhaps it's a good idea i'am going to try it and give you notice if it works – Ahlem Jarrar Aug 17 '16 at 08:29
  • 1
    @AhlemJarrar : Even in the BaseAdapterClass? in the Update method? Just trying to be thorough here :) – Paul Aug 17 '16 at 08:37
  • If you are using Sqlite, then use a CursorAdapter, not a BaseAdapter – OneCricketeer Aug 17 '16 at 08:45
  • @cricket_007 even if i used a CursorAdapter i faced the same problem – Ahlem Jarrar Aug 17 '16 at 09:04
  • @Paul yes Even in the BaseAdapterClass and in the Update method ? the fragments are not refreshing – Ahlem Jarrar Aug 17 '16 at 09:05
  • @Paul i figured out that i was not using notifyDataSetInvalda‌​‌​ted() correctly thanks for your help ! i just add it again in my updateView method and it works ! thunk you very much – Ahlem Jarrar Aug 17 '16 at 10:17
  • 1
    @AhlemJarrar Glad I could help – Paul Aug 18 '16 at 07:55

1 Answers1

1

Thanks to Paul answer the idea was to add the notifyDataSetInvalidated in my UpdateView() Method in my adapter and it works just fine and my fragments are refreshing correctly now :

void notifyDataSetInvalidated () 

the method Notifies the attached observers that the underlying data is no longer valid or available. Once invoked this adapter is no longer valid and should not report further data set changes.

refer link : notifyDataSetInvalidated

Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33