F
F
Fotonick2017-03-14 16:04:45
Android
Fotonick, 2017-03-14 16:04:45

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();

which has 4 tabs with other snippets
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;
        }

    }

SkidkiFragment() is a ListFragment which is normally populated with data from the server the first time it is run. Also, when using an additional filter, the content of this fragment is normally updated (that is, two states - in one a list of 5 items, in the other more than 100).
2. We go to another item of the side menu (replacing MainFragment with some other one)
3. We return to the first screen again through the side menu
MainFragment mainFragment = new MainFragment();
transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.mainContainer, mainFragment, "mainFragment").commit();

4. The first tab (SkidkiFragment()) is filled with data with the filter conditions that were saved when switching from the first screen to another menu item.
Problem - changing the filter conditions now does not work on the list. Although the data loading method gets the data correctly according to the filter change.
As I understand it, the problem is in assigning the setListAdapter adapter. It looks like after "reverting" (recreating the MainFragment with the SkidkiFragment() it contains), the adapter assignment only fires once on creation (populating the data with the old filter conditions). And when the filter is updated, the adapter can no longer be applied to the fragment, which is very strange, since, as I already said, when the application is first loaded, the filter works fine and the adapter assignment does not experience problems. This is only after we went to another screen through the menu item and then returned back.
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

1 answer(s)
F
Fotonick, 2017-03-14
@Photonick

It turned out that this is this bug https://code.google.com/p/android/issues/detail?id...
it was necessary to use not getFragmentManager in the ViewPager adapter, but
mSectionsPagerAdapter = new SectionsPagerAdapter(getChildFragmentManager());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question