0

I have looked in several topics like this but didn't see how to fix my problem

I have a resturant for say.. with dynamic number of categories.. I put all the categories in a list.. and create fragments by from those categories

so I cant just

case 0 : fragment0 case 1 :frament 1

because I dont know how much categories I have untill runtime

class MyPageAdapter extends FragmentStatePagerAdapter {

private List<MyFragment> fragments = new ArrayList<>();
private List<menuCat> Categories = new ArrayList<>();

public MyPageAdapter(FragmentManager fm, List<menuCat> Categories) {
    super(fm);
    this.Categories = Categories;
    for (int i = 0; i<Categories.size();i++)
    {
        fragments.add(MyFragment.newInstance(Categories.get(i)));
    }
}

@Override
public String getPageTitle(int position)
{
    return Categories.get(position).catName();
}

@Override
public MyFragment getItem(int position) {
    return this.fragments.get(position);
}

@Override
public int getCount() {
    return this.fragments.size();
}

MyFragment.class

public class MyFragment extends Fragment {

public static final MyFragment newInstance(menuCat category)
{
    Bundle bun = new Bundle();
    bun.putString("category", category.toJson());
    MyFragment f = new MyFragment();
    f.setArguments(bun);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_my, container, false);
    String json = getArguments().getString("category");
    menuCat category = menuCat.fromJson(json);

    System.out.println(category.catName());

    ArrayList<Card> cards = new ArrayList<Card>();

   for(menuItem item : category.getItems())
   {
       Card card = new Card(getActivity());
       // Create a CardHeader
       CardHeader header = new CardHeader(getActivity());
       // Add Header to card
       header.setTitle(item.getName());
       card.setTitle(item.getPrice());
       card.addCardHeader(header);

       CardThumbnail thumb = new CardThumbnail(getActivity());
       //thumb.setDrawableResource(listImages[i]);
       //card.addCardThumbnail(thumb);

       cards.add(card);
   }


    CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);

    CardListView listView = (CardListView) getActivity().findViewById(R.id.myList);
    if (listView != null) {
        listView.setAdapter(mCardArrayAdapter);
    }
    return v;

}

}

this is my adapter, my problem is for example

if I am in page 1 and I need page 3 data.. if I will go to page 2.. it will display data of page 3 OR if I go to page 5 then go backward.. to 4 it will also show data of page 3.. I mean it gets me like the data of the next page instead of current one.

Anonymous
  • 77
  • 1
  • 9

1 Answers1

0

Creating all of your fragments in the constructor is very poor design, as you're creating references to these objects which will later be attached to an Activity, but when they are detached, you continue to hold the reference. In the end, this is going to cause you a lot of frustration with memory leaks.

Is it not possible to simply remove fragments and change your methods to the following:

@Override
public MyFragment getItem(int position) {
    return MyFragment.newInstance(Categories.get(position));
}

@Override
public int getCount() {
    return Categories.size();
}

I'm not certain this will solve all of your problems, but it is a start.

Joseph Roque
  • 5,066
  • 3
  • 16
  • 22