Forgive my ignorance of Android. I'm just learning. I'm building an app that uses Google's SlidingTabsColors sample code for the main layout (The developer website) .
I've got the layout working the way I want it to with dummy data. The idea is each tab will store a user's score for each round and then display their total score so far at the bottom.
Knowing what's where is helpful. The ListView
and TextView
are in the ViewPager
, the Button
at the bottom is part of the fragment containing the tabs and the ViewPager
. Currently, when the button is pressed, a new entry is added to each player's data set (an ArrayList<Integer>') with the value of
0`. The user can then tap an item and edit it.
That's what works. But the ListView
doesn't update unless it's told to. So when you click the button, there is no change to the UI until you swipe to the next item and the fragment is refreshed. So everything is being added correctly, I just don't know how to tell the current ListView
to update.
Here's the code for the addRound()
method in the parent fragment:
public void addRound() {
// add a blank round for each player in their dataset
for (int i = 0; i < mData.size(); i++) {
mData.get(i).second.add(0);
}
// get the pager adapter
DefaultGameFragmentPagerAdapter adapter = (DefaultGameFragmentPagerAdapter) mViewPager.getAdapter();
int pos = mViewPager.getCurrentItem();
ContentFragment frag = (ContentFragment) adapter.getItem(pos);
if (frag != null) {
frag.update();
}
}
And the update()
method in ContentFragment
:
public void update() {
mAdapter.notifyDataSetChanged();
}
I get a NullPointerException
when I try to access my custom ArrayAdapter, but if I'm getting the current fragment from the ViewPager
, shouldn't all those members be initialized? (The ListView
is set up in the onViewCreated()
method)
Thanks for taking the time to read!
Justin