0

Hi I've started learning android recently and am Trying to get familiar with RecyclerView.

I have a Main menu which is a simple list. When clicked on any item a new activity named subMenu starts. This SubMenu contains a RecyclerView where each item is a cardview having two buttons, that can be used to increase or decrease items quantity (which is also being shown card view). Now I want to retain changes made in Recyclerview for each main menu. Currently I have adapter for only one sub menu.

Any help or sample is appreciated.

Main menu xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainMenu">

<ListView
    android:id="@+id/categories_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar">

</ListView>

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Main menu .java file

public class MainMenu extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    TextView textView=findViewById(R.id.editText);
    Toolbar toolbar=findViewById(R.id.toolbar);
    toolbar.setTitle("Table "+getIntent().getStringExtra("table_no"));
    setSupportActionBar(toolbar);
    final ListView listView=findViewById(R.id.categories_list);
    ArrayAdapter<String> adapter= new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.main_categories));
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent=new Intent(MainMenu.this,subMenu.class);
 intent.putExtra("main_category",listView.getItemAtPosition(position).toString());
            startActivity(intent);
        }
    });
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.custom_menu,menu);
    return true;
}
}

sub menu .xml file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".subMenu">

<Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">

</Toolbar>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview_submenu"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar2"> 
</android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

Edit

To make my question more clear

Here is main menu

enter image description here

When clicked sub menu opens

enter image description here

The number on top right is quantity that can be incremented or decremented by user as shown below

enter image description here

Now when user moves to main menu and comes back to sub menu of same category these changes in quantity are not retained.

I hope I made myself clear. Simple example would be helpful.

Edit

Adapter code for sub menu RecyclerView.

public class RecyclerViewAdapterSubItems extends RecyclerView.Adapter<RecyclerViewAdapterSubItems.subItemViewHolder> {

private Context context;
private List<SubItems> subItemsList;

public RecyclerViewAdapterSubItems(Context context, List<SubItems> subItemsList){
    this.context=context;
    this.subItemsList=subItemsList;
}
@Override
public subItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view;
    LayoutInflater inflater=LayoutInflater.from(context);
    view=inflater.inflate(R.layout.cardview_sub_menu_item,parent,false);
    return new subItemViewHolder(view);
}

@Override
public void onBindViewHolder(final subItemViewHolder holder, final int position) {
    holder.subItemName.setText(subItemsList.get(position).getName());
    holder.subItemPrice.setText(Integer.toString(subItemsList.get(position).getPrice()));
    holder.subItemQuantity.setText(Integer.toString(subItemsList.get(position).getQuantity()));

    holder.incrementQuantityButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            subItemsList.get(position).setQuantity(subItemsList.get(position).getQuantity()+1);
            holder.subItemQuantity.setText(Integer.toString(subItemsList.get(position).getQuantity()));
        }
    });


}

@Override
public int getItemCount() {
    return subItemsList.size();
}

public static class subItemViewHolder extends RecyclerView.ViewHolder {
    TextView subItemName;
    TextView subItemPrice;
    TextView subItemQuantity;
    ImageButton incrementQuantityButton;
    ImageButton decrementQuantityButton;


    public subItemViewHolder(View itemView) {
        super(itemView);

        subItemName=itemView.findViewById(R.id.textView_sub_item_name);
        subItemPrice=itemView.findViewById(R.id.textView_sub__item_price);

subItemQuantity=itemView.findViewById(R.id.textView_sub_item_quantity);


incrementQuantityButton=itemView.findViewById(R.id.imageButton_increment_quantity);

 decrementQuantityButton=itemView.findViewById(R.id.imageButton_decrement_quantity);
        }
    }
    }

submenu.java

    public class subMenu extends AppCompatActivity {

    static List<SubItems> subItemList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_menu);
        RecyclerView recyclerView=findViewById(R.id.recyclerview_submenu);
        subItemList=new ArrayList<>();
        subItemList.add(new SubItems("Pasta",20,0));
        subItemList.add(new SubItems("Peparoney",20,0));
        subItemList.add(new SubItems("asta",40,0));
        RecyclerViewAdapterSubItems adapterSubItems=new RecyclerViewAdapterSubItems(this,subItemList);
        recyclerView.setAdapter(adapterSubItems);
        recyclerView.setLayoutManager(new GridLayoutManager(this,1));
    }
}
Nouman Ahmad
  • 326
  • 3
  • 15
  • Add SubMenu Activity code – nitinkumarp Aug 21 '18 at 09:57
  • just wait 1 min – Nouman Ahmad Aug 21 '18 at 10:03
  • You have to store the values of each count of Submenu into the MainMenu. And if the user again click on menu item then you have to pass the count value with the intent. If user change the value then you have to again update the same Mainmenu count. For communication between activities check [this](https://stackoverflow.com/a/21393369/2196176) – Sunny Aug 21 '18 at 10:43

2 Answers2

0

Take an array of Boolean type with size equal to the size of your main menu items. Initialise all values to false.

And whenever you select any main menu, if it opens, set that position in your array as true and if it closes, set the place as false.

On bindViewHolder add a condition like if the array value is true, show the sub menu else hide the sub menu

And you can also store this array in database to retain the value if you need it even after you kill your application. Let me know if you need help with the code.

Sunny
  • 3,134
  • 1
  • 17
  • 31
Jaswant Singh
  • 9,900
  • 8
  • 29
  • 50
0

Your issue is here:

RecyclerView recyclerView=findViewById(R.id.recyclerview_submenu);
    subItemList=new ArrayList<>();
    subItemList.add(new SubItems("Pasta",20,0));
    subItemList.add(new SubItems("Peparoney",20,0));
    subItemList.add(new SubItems("asta",40,0));

You are creating new SubItems everytime you start your SubMenuActivity. You need to save these subItems permanently for every category.

One of the way is to create a Category POJO having List<SubItems> and initialize them at the MainMenu Activity and pass that Category POJO on itemClick.

nitinkumarp
  • 2,120
  • 1
  • 21
  • 30