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
When clicked sub menu opens
The number on top right is quantity that can be incremented or decremented by user as shown below
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));
}
}