user3146965 user3146965 - 1 month ago 15
Android Question

viewpager first two pages the same

I have arrays which i want to pass through to a nested fragment which will display the correct data depending on the current position. However the first two pages displayed are the same, debugging it seems to show that page 0 is skipped and data from pos 1 of the arrays is loaded into the first two pages.

After swiping through a couple of pages and swiping back to the start it displays correctly, but the initial setup doesnt.

This is my adapater embedded into the outer fragment:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {

public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);

}

@Override
public ScreenSlidePageFragment getItem(int position) {
Bundle bundle = new Bundle();

int logo=logos[position];
String win=wins.get(position);
String draw=draws.get(position);
String loss=losses.get(position);
String team=teams.get(position);
String data=output.toString();

Log.e("check pos outer", "" + position+" "+teams.get(position));

return ScreenSlidePageFragment.newInstance(logo,win,draw,loss,team,data);
}

@Override
public int getCount() {
return NUM_PAGES;
}
}


and this is my nested fragment:

public class ScreenSlidePageFragment extends Fragment {
private static JSONArray data;
private TableLayout tableLayout;
private LayoutInflater inflate;
private static String wins;
private static String draws;
private static String losses;
private static String teams;
private static int logo;

public static ScreenSlidePageFragment newInstance(int logos,String win,String draw,String loss,String team,String output) {
ScreenSlidePageFragment fragmentFirst = new ScreenSlidePageFragment();
Bundle bundle = new Bundle();
bundle.putInt("logo", logos);
bundle.putString("wins", win);
bundle.putString("draws", draw);
bundle.putString("losses", loss);
bundle.putString("teams",team);
bundle.putString("data", output);
fragmentFirst.setArguments(bundle);
return fragmentFirst;
}


public ScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
logo = getArguments().getInt("logo");
wins= getArguments().getString("wins");
draws= getArguments().getString("draws");
losses= getArguments().getString("losses");
teams= getArguments().getString("teams");
String stringData= getArguments().getString("data");
try {
data=new JSONArray(stringData);
} catch (JSONException e) {
e.printStackTrace();
}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
tableLayout=(TableLayout)rootView.findViewById(R.id.squads_tables);
inflate=inflater;
fillTable();

return rootView;
}

private void fillTable() {
if (teams.length() > 0) {

View tr = inflate.inflate(R.layout.squadrow, null,false);
ImageView teamLogo=(ImageView)tr.findViewById(R.id.imageView);
Drawable logoDraw= ContextCompat.getDrawable(getActivity(), logo);
teamLogo.setImageDrawable(logoDraw);
TextView teamname=(TextView)tr.findViewById(R.id.name);
teamname.setText(teams);
TextView record=(TextView)tr.findViewById(R.id.textView3);
record.setText("wins: " + wins + " draws: " + draws + " lost: " + losses);
tableLayout.addView(tr);

try {
int currPos=0;
boolean passed=false;
playerloop:
for(int j=0;j<data.length();j++,currPos++){
JSONObject jsonobject = data.getJSONObject(j);
String currTeamName=jsonobject.getString("name");
String playerName=jsonobject.getString("Name");
String position=jsonobject.getString("position");
int age=jsonobject.optInt("Age", 0);
int played=jsonobject.optInt("played", 0);
int goals=jsonobject.optInt("goals", 0);
if(!currTeamName.equals(teams) && passed){

break playerloop;
}
else if(currTeamName.equals(teams) && !playerName.equals("null")){
if(!passed){
passed=true;
}



View tr_player = inflate.inflate(R.layout.player_row, null,false);
TextView currPlayer=(TextView)tr_player.findViewById(R.id.name_txt);
currPlayer.setText(playerName);

TextView currPosiotion=(TextView)tr_player.findViewById(R.id.textView2);
currPosiotion.setText(position);

TextView currAge=(TextView)tr_player.findViewById(R.id.agetxt);
currAge.setText("Age: "+age);

TextView currMatches=(TextView)tr_player.findViewById(R.id.matchesTxt);
currMatches.setText("Matches: "+played);

TextView currGoals=(TextView)tr_player.findViewById(R.id.textView4);
currGoals.setText("Goals: "+goals);

tableLayout.addView(tr_player);
}

}

} catch (JSONException e) {
e.printStackTrace();
}

}




}


}

Ive tried different approaches such as returning new instance and passing bundle of data but none seem to fix it.

Answer
private static JSONArray data;
private TableLayout tableLayout;
private LayoutInflater inflate;
private static String wins;
private static String draws;
private static String losses;
private static String teams;
private static int logo;

All the members of your fragment class are static. That's probably not what you want because this means that they are shared for all fragment instances.

On initialization the ViewPager will create the current page and the next page. Because the members are shared this might lead to both pages using the same data.

So: Just remove the 'static' keyword. I don't see why you would need that here.