如何保存CheckBox Checked Item如果我滚动

时间:2017-06-19 15:50:45

标签: java android

我创建了一个包含复选框的RecycleView 当我检查项目并滚动检查项目将被取消选中它不会保存我将在图片中解释的动作 这是在滚动之前检查第一项enter image description here时 滚动后 它更改为默认为unChecked enter image description here

和我的适配器类

package abtech.waiteriano.com.waitrer.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import java.util.ArrayList;

import abtech.waiteriano.com.waitrer.R;
import abtech.waiteriano.com.waitrer.getters_and_setters.ItemsListsModel;

/**
 * Created by yassin on 5/17/17.
 */

public class RecycleAdapterDialogModifier extends RecyclerView.Adapter<RecycleAdapterDialogModifier.MyViewHolder> {
    private Context context;
    ArrayList<ItemsListsModel> modifierArrayList;
    ArrayList<Boolean> positionArray;


    public RecycleAdapterDialogModifier(Context context, ArrayList<ItemsListsModel> modifierArrayList) {
        this.context = context;
        this.modifierArrayList = modifierArrayList;
        positionArray = new ArrayList<Boolean>(modifierArrayList.size());
        for(int i =0;i<modifierArrayList.size();i++){
            positionArray.add(false);
        }
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.modifierlistitem, parent, false);


        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        ItemsListsModel itemsListsModel = modifierArrayList.get(position);
        holder.modifierCB.setText(itemsListsModel.getName());

        holder.modifierCB.setFocusable(false);
        holder.modifierCB.setChecked(positionArray.get(position));
        holder.modifierCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked ){
                    System.out.println(position+"--- :)");
                    positionArray.set(position, true);

                }else
                    positionArray.set(position, false);
            }
        });
    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder {
        CheckBox modifierCB;
        TextView usernameContItem;


        public MyViewHolder(View itemView) {
            super(itemView);
            modifierCB = (CheckBox) itemView.findViewById(R.id.itemCb);

        }
    }
}
抱歉我的英语不好,如果有什么不清楚的话

这是Class Calls Adapter中的一部分

private static void setPiking(String modeCode) {
    RecyclerView modifierItemList;
    Button OkBtn, CancelBtn;
    LayoutInflater inflater2 = (LayoutInflater)  context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view2 = inflater2.inflate(R.layout.modifier_list, null);
    modifierItemList = (RecyclerView) view2.findViewById(R.id.modifierList);
    OkBtn = (Button) view2.findViewById(R.id.okBtn90);
    CancelBtn = (Button) view2.findViewById(R.id.cancelBtn90);
    final AlertDialog.Builder builder1 = new AlertDialog.Builder(new ContextThemeWrapper(context, R.style.myDialog));

    ModGroubeCode = Integer.parseInt(modeCode);
    String Sql = "SELECT  dbo.Items.Code, dbo.Items.Name FROM dbo.Items";
    ResultSet RS = ConnectionClass.Ret_RS(Sql);
    try {

            ArrayList<ItemsListsModel> modifierArrayList = new ArrayList<ItemsListsModel>();
            while (RS.next()) {

                builder1.setMessage("Modifier Group :- " + RS.getString("ModifierGroup_Name"));
                String name = RS.getString("Name");
                String id = RS.getString("Code");
                TreeMap<String, Object> arr = new TreeMap<String, Object>();



                ItemsListsModel itemsListsModel = new ItemsListsModel(id, name, arr);
                modifierArrayList.add(itemsListsModel);
            }
            adapter = new RecycleAdapterDialogModifier(context, modifierArrayList);
            modifierItemList.setAdapter(adapter);

        builder1.setView(view2);
        builder1.setCancelable(true);

        final AlertDialog alert11 = builder1.create();

        OkBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        CancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert11.dismiss();
            }
        });


        alert11.show();


        if (Mo_multiPick) {
            //show Ok
            OkBtn.setVisibility(View.VISIBLE);

        } else {
            OkBtn.setVisibility(View.INVISIBLE);
        }
        if (Mo_allawNoPick) {
            //show Cancel
            CancelBtn.setVisibility(View.VISIBLE);
        } else {
            CancelBtn.setVisibility(View.INVISIBLE);
        }
    } catch (Exception ex) {

    }
}

1 个答案:

答案 0 :(得分:0)

尝试在itemHolder.checkBox.setOnCheckedChangeListener(null);之前写holder.modifierCB.setChecked(positionArray.get(position));,以确保不会触发OnCheckedChangeListener()