andrew
andrew

Reputation: 3899

ListView, Adapters and Tabs

I'm an android beginner

I have an activity with FixedTabs + Swipe. Each tab has a ListView inside it, I use an adapter to populate it, cause I'm using a custom object.

It works, but everytime I navigate the tabs, content is added more times. For example: -the first tab displays A and B. -then I navigate to the second, then the third -when I come back to the first tab, I see A,B,A,B.

I taught to solve with saveInstanceState but I see that's not working, saveInstanceState is always null Here's the code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_categories_dummy, container, false);

    categoryListView = (ListView) rootView.findViewById(R.id.categoryListView);



    if (savedInstanceState != null) {
        category = savedInstanceState.getString("FRAGMENT_CATEGORY");
    } else {
        // Get the category
        switch (getArguments().getInt(ARG_SECTION_NUMBER) - 1) {
        case 0:
            category = "First";
            break;
        case 1:
            category = "Second";
            break;
        case 2:
            category = "Third";
            break;
        }
        getArrayList();
        categoryListView.setAdapter(new TweetsAdapter(getActivity(), R.layout.category_row, arrayList, getArguments().getInt(ARG_SECTION_NUMBER)));
    }

    return rootView;
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putString("FRAGMENT_CATEGORY", category);
}

Upvotes: 0

Views: 208

Answers (1)

ayon
ayon

Reputation: 2180

You have forgot to clear your Array or ArrayList whatever your are using to hold your List items when you change the tabs. I guess , when you change your tabs you just add the relevant elements of List corresponding to that Tab , but you have to clear it first before you add new items to it again. Otherwise new Items will be added to the list of old Items. That's why , you are seeing old items. Hope it helps.

Upvotes: 2

Related Questions