Ahlem Jarrar
Ahlem Jarrar

Reputation: 1129

Communicate between Adapter, fragments and Sqlite Data base to refresh Fragments

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.

Upvotes: 0

Views: 328

Answers (1)

Ahlem Jarrar
Ahlem Jarrar

Reputation: 1129

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

Upvotes: 1

Related Questions