简体   繁体   English

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

[英]How to save CheckBox Checked Item If I scroll

I have created a RecycleView contains check boxes when I check Item and scrolls the check item gonna be unchecked it does not save the action I gonna explain in the pic this is before scrolling when checked the first item enter image description here after scrolling it changed to default as unChecked enter image description here 我创建了一个RecycleView包含复选框,当我检查项目并滚动检查项目将被取消选中它不保存动作我将在图片中解释这是在滚动之前检查第一项目在滚动后输入图像描述更改为默认为unChecked 在此处输入图像说明

and here's My adapter Class 这是我的适配器类

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

        }
    }
}

sorry for my bad english and If there's any thing is not clear 抱歉我的英语不好,如果有什么不清楚的话

this is the part in Class Calls Adapter 这是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) {

    }
}

Try writing itemHolder.checkBox.setOnCheckedChangeListener(null); 尝试编写itemHolder.checkBox.setOnCheckedChangeListener(null); just before holder.modifierCB.setChecked(positionArray.get(position)); 就在holder.modifierCB.setChecked(positionArray.get(position));之前holder.modifierCB.setChecked(positionArray.get(position)); to ensure that OnCheckedChangeListener() is not triggered. 确保不触发OnCheckedChangeListener()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将状态保存到选中项? - How to save the state to a checked item? 如何保存选中的列表视图项 - How to save listview item checked 如何保存 CheckedTextView 复选框的状态,以便在退出后保持选中状态? - How do I save the state of CheckedTextView checkbox so it stays checked after exiting? GXT:如何设置Checkbox TreeGrid项目最初被选中? - GXT: How to set Checkbox TreeGrid item initially checked? 选中复选框后如何将项目移动到最后一个位置? (RecyclerView,ViewHolder) - How to move item to the last position when Checkbox is checked? (RecyclerView, ViewHolder) 如何将项目添加到列表 <Integer> 在Android上选中复选框时 - How to add item into List<Integer> when checked checkBox on Android Android-如何在更改活动后保存复选框状态(已选中/未选中) - Android - How to save checkbox state (checked/unchecked) after changing activity 选中复选框并按下按钮时如何保存其状态 - How to save the state of the checkbox when it is checked and a button pressed 如何检查复选框是否已选中? - How to check if checkbox was checked? 选中复选框后滚动时,复选框会随机选中和取消选中 - checkboxes gets randomly checked and unchecked when i scroll after checking a checkbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM