I use the example of a pager enter link description here
I have everything worked out but there is one problem. I have 4 different fragments. One of them contains ListView. When I click on an item in the ListView, change the global variable guestId
. If then return to other fragments, these they should be changed. Because they use this variable as a parameter. But they do not change.
public class Guest extends Fragment {
...
private ViewPager pager;
private MyPagerAdapter adapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.guest, null);
...
if (InternetResiver.isOnline(getActivity())) {
setData();
} else {
AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
alert.show();
}
return v;
}
public void startNewTread(String newId) {
guestId = newId;
Log.d("usernewid",newId);
setData();
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return Folio.newInstance(guestId,hotel,room);
case 1: return BalanceDetail.newInstance(guestId,hotel);
case 2: return History.newInstance(guestId,hotel);
case 3: return Record.newInstance(guestId,hotel,room);
default: return Folio.newInstance(guestId,hotel,room);
}
}
@Override
public int getCount() {
return 4;
}
}
public void setData() {
String link = String.format(......., guestId, hotel);
new MyAsincTask(getActivity()){........}.execute(link);
}
}
my fragment
public class Folio extends Fragment {
private ArrayList<FolioBean> folioBeans;
private TableLayout tableFolio;
private String guestId;
private String hotel;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.folio, null);
guestId = getArguments().getString("guestid");
hotel = getArguments().getString("hotel");
folioBeans = new ArrayList<>();
tableFolio = (TableLayout) v.findViewById(R.id.tableFolio);
if (InternetResiver.isOnline(getActivity())) {
setData();
} else {
AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
alert.show();
}
return v;
}
public static Folio newInstance(String guestid,String hotel,String room) {
Folio f = new Folio();
Bundle b = new Bundle();
b.putString("guestid", guestid);
b.putString("hotel", hotel);
b.putString("room", room);
f.setArguments(b);
return f;
}
public void setData() {
String link = String.format(........., guestId, hotel);
new MyAsincTask(getActivity()){........}.execute(link);
}
}
other fragments are loaded using the same guestId