mardi 3 décembre 2019

How to save data from nested RecyclerView [duplicate]

Whenever the checkbox is set to true, I would like to store all of the Item Names and Sub Item names that are currently selected on the screen. I’m trying to obtain the values of the Item and Sub Item but I’m struggling to figure out how to retrieve the data from the recycler views, especially since it is nested. Here is an example of what I have:

enter image description here

I just want to be able to store the information from the adapters so that I can use it for later whether it’s in MainActivity or another Activity. I’m thinking I can use a hash map to store the Item and Sub Item names if the checkbox is selected, but I don’t really know how I can do that here. I saw that SharedPreferences might also be helpful but I’m also unfamiliar with how to implement it for this. This is what the code looks like:

MainActivity

package com.example.recycledviewpoolexample;

import android.nfc.Tag;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RecyclerView rvItem = findViewById(R.id.rv_item);
        LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        ItemAdapter itemAdapter = new ItemAdapter(buildItemList());
        rvItem.setAdapter(itemAdapter);
        rvItem.setLayoutManager(layoutManager);
    }

    private List<Item> buildItemList() {
        List<Item> itemList = new ArrayList<>();
        for (int i=0; i<10; i++) {
            Item item = new Item("Item "+i, buildSubItemList());
            itemList.add(item);
        }
        return itemList;
    }

    private List<SubItem> buildSubItemList() {
        List<SubItem> subItemList = new ArrayList<>();
        for (int i=0; i<3; i++) {
            SubItem subItem = new SubItem("Sub Item "+i, false);
            subItemList.add(subItem);
        }
        return subItemList;
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume");

        /*if Checkboxes are true, store/display them in the logs

        EXAMPLE OUTPUT:
        Item (String)
        SubItem (String)
         */
    }
}

SubItemAdapter.java

package com.example.recycledviewpoolexample;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import java.util.List;

public class SubItemAdapter extends RecyclerView.Adapter<SubItemAdapter.SubItemViewHolder> {

    private List<SubItem> subItemList;

    SubItemAdapter(List<SubItem> subItemList) {
        this.subItemList = subItemList;
    }

    @NonNull
    @Override
    public SubItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_sub_item, viewGroup, false);
        return new SubItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SubItemViewHolder subItemViewHolder, int i) {
        final SubItem subItem = subItemList.get(i);
        subItemViewHolder.tvSubItemTitle.setText(subItem.getSubItemTitle());
        subItemViewHolder.checkBox.setOnCheckedChangeListener(null);
        subItemViewHolder.checkBox.setChecked(subItem.isSelected());

        subItemViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                subItem.setCheckbox(isChecked);

            }
        });
    }

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

    class SubItemViewHolder extends RecyclerView.ViewHolder {
        TextView tvSubItemTitle;
        Switch checkBox;

        SubItemViewHolder(View itemView) {
            super(itemView);
            tvSubItemTitle = itemView.findViewById(R.id.tv_sub_item_title);
            checkBox = itemView.findViewById(R.id.switch1);
        }
    }
}

SubItem.java

package com.example.recycledviewpoolexample;

public class SubItem {
    private int subItemImage;
    private String subItemTitle;
    private boolean checkbox;

    public SubItem(String subItemTitle, boolean checkbox) {
        this.subItemTitle = subItemTitle;
        this.checkbox = checkbox;
    }

    public int getSubItemImage() {
        return subItemImage;
    }

    public void setSubItemImage(int subItemImage) {
        this.subItemImage = subItemImage;
    }

    public String getSubItemTitle() {
        return subItemTitle;
    }

    public void setSubItemTitle(String subItemTitle) {
        this.subItemTitle = subItemTitle;
    }

    public boolean isSelected() {
        return checkbox;
    }

    public void setCheckbox(boolean checkbox) {
        this.checkbox = checkbox;
    }
}



Aucun commentaire:

Enregistrer un commentaire