0

I have a Drawer Listview with items Fruits, Veggies, Meat. Each has a class that extends ListFragment. I also created a Product class for my ArrayList and ArrayAdapter. But everytime i switch between ListFragment and switch back, the changes I did also reset and not saved.

Here is my Current codes:

product_adapter.java

//imports ....

public class product_adapter extends ArrayAdapter<product> {

    public product_adapter(Context context, ArrayList<product> prod) {
        super(context, 0, prod);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final product products = getItem(position);

        // Some Codes for Instantiation and LayoutInflater


        itemAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                products.itemQuantity ++;
                viewHolder.itemQuantity.setText(String.valueOf(products.getItemQuantity()));
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
}

Fruit.java

public class Fruit extends ListFragment {

    public ArrayList<product> products;
    public product_adapter productAdapter;

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

        products = new ArrayList<product>();

        products.add(new product("Apple", 18.00 /*Price*/, 0 /*Quantity*/));
        products.add(new product("Orange", 16.00 /*Price*/, 0 /*Quantity*/));

        productAdapter = new product_adapter(getActivity(),products);

        setListAdapter(productAdapter);
    }
}

Veggies and Meat Class is constructed the same way as the Fruit class

MainActivity with the ListView

//Some codes here for the creating ListView
        listView = (ListView) findViewById(R.id.categoryView);
        listView.setAdapter(new category_adapter(this, categories));
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerView);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                ListFragment listFragment = null;
                switch (position){
                    case 0:
                        listFragment = new Fruit();
                        break;
                    case 1:
                        listFragment = new Veggies();
                        break;
                    case 2:
                        listFragment = new Meat();
                        break;
                }

                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().replace(R.id.itemLayout,listFragment).commit();
                drawerLayout.closeDrawers();
            }
        });
mark.jon
  • 107
  • 12

1 Answers1

0

@mark.jon Here is the code implemented by me for producing categorised list on drawer using expandable list.. Hope this fulfills your needs.. Default Navigation Drawer View to ExpandableListView

Community
  • 1
  • 1
Moulesh
  • 2,762
  • 1
  • 22
  • 32