ListView SimpleCursorAdapter FragmentPagerAdapter Tabs

25 Views Asked by At

I thought long and hard about how to represent this problem. I thought that there is too much code but that I can describe it simply. And that this will probably tip someone off as to where the problem may be.

Android Studio 3.5.1.

I have created a simple AppCompatActivity that loads a new Tab whenever the user presses a "+" button in the menu. The PagerAdapter loads a new fragment with a ListView backed by a SimpleCursorAdapter. This test code loads the Table (TabID, Name, Count) with dummy data for 20 rows hooks it up and displays the data. So far so good. One Tab loaded with a ListView of dummy data showing Name and Count in the ListViewItem layout file. Cool. Wired up an onItemClick event for the ListView in the Fragment, so that when the user clicks on a row, the count is incremented in the database for the correct TabID, then I refresh the Cursor, call adapter.changeCursor() followed by adapter.notifyDataSetChanged(). This works beautifully. Every tap on a row increments the count on the row and displays it.

Now, I click "+" to add my second Tab. A new tab appears, dummy data is loaded, and the count field is at zero for each row. I can swipe left/right back and forth between the tabs clicking rows and incrementing counts. The counts are maintained and displayed correctly.

Here is where the problem starts. I click "+" again to add the third Tab. It is loaded with dummy data and count field set to zero. Good. Swipe back to Tab #2 and all the fields I incremented remember their counts. Swipe back to Tab #1, however, and all the counts are lost! Back to zero. Go to Tab #2, all the counts are there. Go to Tab #3, and all the counts are back to zero. It seems that if I have 3 or more Tabs, and go back and forth between adjacent Tabs; no problems. But if I Tab from 1 to 3, counts in Tab #1 are lost; and vice versa, if I Tab from 3 to 1, counts in Tab #3 are lost.

I hope I have made this as clear and concise as possible and that someone knows why this might be breaking on Tab #3 +.

0

There are 0 best solutions below