2

I created a custom listview with baseadapter.The list items contains a checkbox for the purpouse of multichoice.I added a function in the baseadapter that returned the positions of the selected items.

To catogarize the listitems i tried to put the listview in Fragments with a Viewpager with FragmentPagerAdapter.Now each Fragment has its own multichoice listview.The user needs to be able to swipe between pages and select items then finally before moving to the next Activity i have to collect all the selected item positions into a two dimensional List or Array.

MyProblem: It seems the Viewpager maintains only 2 additional pages in memory one on either side of the current page.If my viewpager is at a corner or has more than 3 pages iam unable to collect the list of selected items in some of the fragments.

I would appreciate any help on this.

Deepak
  • 1,238
  • 3
  • 22
  • 47

1 Answers1

0

Have each Fragment save the selected items to SharedPreferences when the user navigates away from it. Later have your other Activity retrieve the items from SharedPreferences.

int len = listView.getCount();
SparseBooleanArray checked = listView.getCheckedItemPositions();
for (int i = 0; i < len; i++)
{
    if (checked.get(i)) 
    {
        // Get the item
        // Deal with the checked item. 
        // ex: add to ArrayList
        arrayList.add(item);
    }
}

Set<String> set = new HashSet<String>();
set.addAll(arrayList);
mSharedPreferences.edit().putStringSet("key", set).apply();
pez
  • 3,859
  • 12
  • 40
  • 72
  • wont the pagechangelistener be firing the events every time i swipe? – Deepak Jul 25 '15 at 17:39
  • @Deepak I would put this logic in the `onPause()` method of each `Fragment`, not in the page change listener. I'm not aware of the structure of your app, though. – pez Jul 25 '15 at 17:43