ListView SimpleCursorAdapter FragmentPagerAdapter Tabs - android

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 +.

Related

How to change content of fragment in pagerview

I have a Page which has three buttons (teens, adult, old) and same page contains view pager which has 7 tabs for week days. When this page loaded it show list of all programs for all (teens, adult and old) categories in all 7 tabs according to which day is. What I want to implement is that when user select anyone one of the above button the contents of all the tabs populated by new filtered data list according to the selected button.
How I can implement this?.
You just need to set the selection in your Adapter and then call notifyDataChanged. That will reload the adapter and go back through the instantiateItem calls again. In instantiateItem you can look at the selection and change the content accordingly.

SwipeView that starts with the layout of an intermediary fragment

How can creat a SwipeView that starts with the layout of an intermediary fragment from the collection instead of the first?
e.g.
I have a list of 10 emails of the same person and I click the 3th most recent to show the content in another Activity.
After that, I want to swipe to left and go to the 2nd most recent and swipe to right and go to 4th most recent.
I am following the tutorial
but it seems that the first layout displayed to the user always will correspond to first position from the collection of Fragments.
In my example the user will just be able to swipe to righ and see the emails from 3 to 10.
You can use ViewPager's setCurrentItem() method to specify the initially shown fragment by index, although you should only do this only on the first initialization (i.e. when savedInstanceState is null) so that the ViewPager can persist it's selection through Activity restarts.

Bug? Programmatically added content not shown in Fragment

i have some strange behavior with the content of a fragment.
I created an app with swipeable tab menu, total 4 fragments/tabs.
On the fourth fragment, i add content (TextViews) programmatically, according to 2 spinner on the top of the fragment. So when you choose a value of one of the spinner, the content (inside a LinearLayout) is being replaced with new generated TextViews.
Everything is working nice, BUT:
When both spinner are on the first value (and just there), the content disapears when i swipe to another fragment and come back to the fourth fragment. After selecting another value from one of the spinners, the content is being generated as normal, also when i select the first values again.
When i swipe the first time to the fourth fragment, i see the content.
Any hint?
Looks like your states of the fragments are not saved correctly.
Try setting:
myViewPager.setOffscreenPageLimit(4);

How to swipe to same Fragment but with other data:?

I'm building this application where I have 2 activities. Both of them consist of 3 fragment - one for title, one for content and one for tab control. It is shown at image below.
First activity serves for showing list of some data's headers (item name etc.), search, app info etc. When user presses item in list, app takes him to another activity to show him detail of chosen item. This "details" activity has 6 different content fragments and user switch between them via buttons in tab control fragment (I did switching between content fragments by showing chosen one and hiding all others - I don't know if it's right way, it's my firs app so it came to my mind at first :) ).
And what I would like to do is: When I'm in detail and I swipe left/right then I want app to take me to previous/next item's detail, to same fragment where I currently was in (so not to next content fragment, but to detail of next item in 1st activity's list).
Is this somehow possible please? Because I have totally no clue how to do it :)
And what I would like to do is: When I'm in detail and I swipe
left/rigt then I want app to take me to previous/next item's detail,
to same fragment where I currently was in (so not to next content
fragment, but to detail of next item in 1st activity's list).
If you want to swipe left-right then you would need a ViewPager widget. I'm not sure how should your details activity behave so I'm providing you with two options. Do you want to be able to switch to the next/previous item's details only when a certain fragment is the one currently viewed by the user(from the 6 content fragments, which I assume are related and show various data for a single item)? If yes then in that desired fragment you would replace the current content of the fragment(which will only act as a container) with a ViewPager and use nested fragments for the actual content. If the user switches to the details of a previous/next item's details and then suddenly wants to see the data for that item from one of the remaining 5 content fragments then you would need to have some updates method on them to refresh the data to show the current item(a OnPageChangeListener will be useful here).
Second option, is if you want to allow the user to swipe left/right from any of the 6 content fragments. If this is the case you would use the same method as above but you'll modify each of those 6 fragments.
Showing the next/previous item is easy, just get some sort of identifier of the data(a position, id), retrieve the whole used data(as in the first activity) and then cycle between it.

Should I use Activities or Views with tabs & lists?

I'm new to Java & Android development. I'm trying to develop an app that has 2 tabs, 1 tab has a listview inside of it. When you click on an item in the list, it takes you to another list.. and then you select another item, onto another list, ect.. until they reach the final page which I have setup as a non-selectable list. My question is.. should I create a new activity every time the user clicks on an item in the list? Or is that something normally done with changing views? If done with views, doesn't that pretty much disable their use of going back with the back button?
In the other tab I have an area with a list I guess in which you can remove items off of the list.. Now would I create a new activity for this and put this tab activity on every activity of the lists? I guess this part is what got me confused.. if I hadn't had the other tab my current setup of creating a new activity as the user drills down the lists worked just fine.
This all might sounds a little confusing but let me know if you guys need further explanation..
I would use changeable views only when the views are conceptually showing the same data from a different viewpoint. Since you say "it takes you to another list", I'd say use a separate activity.
As for the tab, my understanding is that you can model each tab as a separate activity, I'm not clear on why you would "put this tab activity on every activity of the lists"? Are you saying that one tab (the "remove" tab) is dependent on what's showing on the other tab ("the list tab")? Without knowing more about the context, but first instinct would be to use a separate "remove tab" activity and model the tab host as having separate activities per tab.

Categories

Resources