mardi 19 avril 2016

Correct way to update lists of data pulled in from a database when working with fragments?

So I've got an SQL helper class that will query the database and return List<MyObjectName> containing all the current data.

In my MainActivity class, I create a private member variable List<MyObjectName> mList = myDbHelper.fillListWithObjects().

Now, I have a ViewPager in my MainActivity, and so each page is a Fragment, one of which is a ListFragment. So I pass my list as an argument to this Fragment via the newInstance() method and it gets stored in a Bundle. I retrieve it from within the Fragment and then fill the list.

In order to communicate data between a Fragment and Activity you use an interface, which I use. So if I want to add or delete an item to this list, I call upon a method that executes from within my Activity body which updates the database accordingly.

Now at this point I have to update the lists, so I re-call fillListWithObjects in MainActivity and then I have to update the ListFragment too, so I created a method in my Fragment that accepts a new list as an argument and then re-creates and re-sets its adapter using the new list as the argument.

This feels like an awfully long-winded way to go about things. Am I doing this correctly, or is there a much better approach?

Aucun commentaire:

Enregistrer un commentaire