如何存储RecyclerView适配器的更改值

时间:2018-08-20 18:40:46

标签: android android-recyclerview

嗨,我最近开始学习android,正在尝试熟悉RecyclerView。

我有一个主菜单,它是一个简单的列表。单击任何项​​目时,将启动一个名为subMenu的新活动。此子菜单包含一个RecyclerView,其中每个项目都是具有两个按钮的卡片视图,可用于增加或减少项目数量(也显示为卡片视图)。现在,我想保留在Recyclerview中对每个主菜单所做的更改。目前,我只有一个子菜单具有适配器。

感谢您的帮助或样品。

主菜单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>

主菜单.java文件

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;
}
}

子菜单.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=".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>

编辑

让我的问题更清楚

这是主菜单

enter image description here

当单击子菜单打开时

enter image description here

右上角的数字是可由用户递增或递减的数量,如下所示

enter image description here

现在,当用户移至主菜单并返回相同类别的子菜单时,这些数量的更改将不保留。

我希望我能说清楚。简单的例子会有所帮助。

修改

子菜单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));
    }
}

2 个答案:

答案 0 :(得分:0)

采用array类型的Boolean,其大小等于主menu items的大小。 将所有值初始化为false。

每当您选择一个主菜单时,如果将其打开,则将array中的位置设置为true,如果关闭,则将位置设置为false

bindViewHolder上添加一个条件,例如array的值为true,则显示sub menu,否则隐藏sub menu

而且,即使您在终止应用程序后仍需要它,也可以将该数组存储在database中以保留该值。 让我知道您是否需要代码方面的帮助。

答案 1 :(得分:0)

您的问题在这里:

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));

每次启动SubMenuActivity时都在创建新的SubItem。您需要为每个类别永久保存这些子项。

一种方法是创建一个具有Category的{​​{1}} POJO,并在MainMenu Activity上对其进行初始化,然后在itemClick上传递该Category POJO。