I have an activity that contains a FragmentPagerAdapter to swipe between multiple fragments (all using the same Fragment Layout). Each Fragment contains two buttons a - and a + used to increment a number in a TextViewA. The value of the TextViewA then alters the value of TextViewB based on its value. So clicking the plus button increases the number of TextViewA, and when it reaches 3, it increments TextViewB.
The problem I am running into is that my code works perfectly on the Fragment1, but when I swipe over to Fragment2, the buttons change the values on Fragment1's TextViews instead of Fragment2's.
Here is the activity:
public class SwipeActivity extends Activity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_swipe);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
Class with FragmentPagerAdapter:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private static final int NUM_PLAYERS = 7;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a SwipeTestFragment.
return SwipeTestFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show NUM_PLAYERS total pages.
return NUM_PLAYERS;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "PLAYER 1";
case 1:
return "PLAYER 2";
case 2:
return "PLAYER 3";
case 3:
return "PLAYER 4";
case 4:
return "PLAYER 5";
case 5:
return "PLAYER 6";
case 6:
return "PLAYER 7";
}
return null;
}
}
Lastly, here is the Fragment class
public class SwipeTestFragment extends Fragment implements View.OnClickListener {
// The fragment argument representing the section number for this fragment.
private static final String ARG_SECTION_NUMBER = "section_number";
private static final int NUM_PLAYERS = 7;
int playerNum;
int[] score = new int[NUM_PLAYERS];
int[] coins = new int[NUM_PLAYERS];
public SwipeTestFragment() {
}
// Returns a new instance of this fragment for the given section number.
public static SwipeTestFragment newInstance(int sectionNumber) {
SwipeTestFragment fragment = new SwipeTestFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
//When creating, retrieve this instance's number from its arguments.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
playerNum = getArguments() != null ? getArguments().getInt(ARG_SECTION_NUMBER) : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View rootView = inflater.inflate(R.layout.fragment_swipe, container, false);
//Set Page Title
TextView playerHeading = (TextView) rootView.findViewById(R.id.playerHeading);
playerHeading.setText("Player " + (playerNum));
//This section sets up buttons.
ImageButton minusone = (ImageButton) rootView.findViewById(R.id.minusBtn1);
minusone.setOnClickListener(this);
ImageButton plusone = (ImageButton) rootView.findViewById(R.id.plusBtn1);
plusone.setOnClickListener(this);
//Populating data arrays with zeros
score[playerNum] = 0;
coins[playerNum] = 0;
return rootView;
}
@Override
public void onClick(View v) {
String presentvaluestring;
int presentvalueint;
int presentVal;
TextView scoreTV = (TextView) getActivity().findViewById(R.id.scoreSevenWonders);
TextView coinsTV = (TextView) getActivity().findViewById(R.id.coinsSevenWonders);
switch (v.getId()) {
case R.id.minusBtn1:
presentVal = coins[playerNum];
if (presentVal > 0) {
presentVal--;
coinsTV.setText(String.valueOf(presentVal));
coins[playerNum] = presentVal;
if (presentVal % 3 == 2) {
setPlayerScore(-1, scoreTV);
}
}
break;
case R.id.plusBtn1:
presentVal = coins[playerNum];
presentVal++;
if ((presentVal % 3) == 0) {
setPlayerScore(1, scoreTV);
}
coinsTV.setText(String.valueOf(presentVal));
coins[playerNum] = presentVal;
break;
}
}
public TextView setPlayerScore(int increment, TextView total) {
String totalstring = total.getText().toString();
int totalint = Integer.parseInt(totalstring);
totalint += increment;
total.setText(String.valueOf(totalint));
return total;
}