Answer the question
In order to leave comments, you need to log in
Why isn't the ListFragment populated when the fragment is called again?
Problem Scenario
1. The application has loaded. The main fragment is loaded (the first item of the NavigationDrawer side menu)
MainFragment mainFragment = new MainFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainContainer, mainFragment, "mainFragment").commit();
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
skidkiFragment = new SkidkiFragment();
return skidkiFragment;
case 1:
lotteriesFragment = new LotteriesFragment();
return lotteriesFragment;
case 2:
actionsFragment = new ActionsFragment();
return actionsFragment;
case 3:
prizesFragment = new PrizesFragment();
return prizesFragment;
default:
return null;
}
}
@Override
public int getCount() {
return 4;
}
}
MainFragment mainFragment = new MainFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainContainer, mainFragment, "mainFragment").commit();
public class SkidkiFragment extends ListFragment {
....
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
skidkiFragment = inflater.inflate(R.layout.fragment_skidki, null);
...
GetSkidki();
...
return skidkiFragment;
}
private void GetSkidki() {
AllConnectService allConnectService = AllConnectService.retrofit.create(AllConnectService.class);
Call<ResponseBody> call;
if (selectID == -1) {
call = allConnectService.getSkidki("json");
} else {
call = allConnectService.getSkidkiByFilter("json", selectID);
}
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
if (response.code() == 200) {
responseJSON = new JSONArray(response.body().string());
getCount = responseJSON.length();
allSkidkiList = new ArrayList();
ClassOneSkidkaPartner classOneSkidkaPartner;
int count = 0;
while (count < getCount) {
classOneSkidkaPartner = new ClassOneSkidkaPartner(
responseJSON.getJSONObject(count).getInt("id"),
responseJSON.getJSONObject(count).getString("name"),
responseJSON.getJSONObject(count).getString("site"),
responseJSON.getJSONObject(count).getString("vk_site"),
responseJSON.getJSONObject(count).getString("ok_site"),
responseJSON.getJSONObject(count).getString("fb_site"),
responseJSON.getJSONObject(count).getInt("ratio"),
responseJSON.getJSONObject(count).getInt("voice"),
responseJSON.getJSONObject(count).getString("description"),
responseJSON.getJSONObject(count).getString("desc_discount"),
responseJSON.getJSONObject(count).getDouble("discount"),
responseJSON.getJSONObject(count).getDouble("discount_max"),
responseJSON.getJSONObject(count).getString("discount_type"),
responseJSON.getJSONObject(count).getString("logo"),
responseJSON.getJSONObject(count).getInt("IsScore")
);
if (userLocalStore.getAddBonuses()) {
if (classOneSkidkaPartner.getIsScore() == 0) {
} else {
allSkidkiList.add(classOneSkidkaPartner);
}
} else {
allSkidkiList.add(classOneSkidkaPartner);
}
count++;
}
if (getCount == 0) {
} else {
adapterOneSkidka = new AdapterOneSkidka(allSkidkiList, getContext());
setListAdapter(adapterOneSkidka);
}
} else {
Toast toast = Toast.makeText(getActivity(), response.code() + response.message(), Toast.LENGTH_SHORT);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast toast = Toast.makeText(getActivity(), "Ошибка! Не удалось получить данные. Проверьте соединение с интернетом.", Toast.LENGTH_SHORT);
toast.show();
}
});
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
if (userLocalStore.getAddBonuses()) {
menu.findItem(R.id.addBonuses).setChecked(true);
} else {
menu.findItem(R.id.addBonuses).setChecked(false);
}
super.onPrepareOptionsMenu(menu);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_search, menu);
inflater.inflate(R.menu.filter_bonus, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addBonuses:
if (item.isChecked()) {
item.setChecked(false);
userLocalStore.setAddBonuses(false);
GetSkidki();
} else {
item.setChecked(true);
userLocalStore.setAddBonuses(true);
GetSkidki();
}
break;
default:
break;
}
return true;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question