当前位置: 首页 > 知识库问答 >
问题:

在android中勾选RecycerView中的复选框后,其他随机复选框将自动勾选

徐麒
2023-03-14
public class LeadListAdapter extends RecyclerView.Adapter<LeadListAdapter.LeadListViewHolder> {

private List<Customer> customerDataList;
private Activity mActivity;
public static List<PEdgeLeads> selectedLeadsList;
public static List<Customer> selectedCustomerList;
private List<PEdgeLeads> leadDataList;
private int dataSize = 0;
public static Context mContextAdapter;

public LeadListAdapter(List<Customer> customerDetails, List<PEdgeLeads> leadDetails, Activity activity) {

    System.out.println("Inside LeadList Adapter Constructor");
    leadDataList = new ArrayList<PEdgeLeads>();
    customerDataList = new ArrayList<Customer>();
    selectedLeadsList = new ArrayList<PEdgeLeads>();
    selectedCustomerList = new ArrayList<Customer>();

    dataSize = leadDataList.size();
    System.out.println("lead list :: " + leadDataList.size() + " and customer data list :: " + customerDataList.size());

    this.customerDataList = customerDetails;
    this.leadDataList = leadDetails;
    System.out.println("Lead Details List :: " + this.leadDataList.get(0).getLeadName());
    System.out.println("Customer Details List :: " + this.customerDataList.get(0).getFirstName());
    this.mActivity = activity;
}

@Override
public LeadListAdapter.LeadListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

    View itemView = LayoutInflater.
            from(viewGroup.getContext()).
            inflate(R.layout.lead_list_view, viewGroup, false);
    System.out.println("Inflating Lead List View NOW");

    return new LeadListViewHolder(itemView);
}

@Override
public void onBindViewHolder(final LeadListAdapter.LeadListViewHolder viewHolder, final int position) {

    viewHolder.leadName.setText(leadDataList.get(position).getLeadName());

    //if (customerDataList.size() < position) {

    if (!customerDataList.get(position).getContactNumber().equals("") || customerDataList.get(position).getContactNumber() != null) {
        final String mobileNumber = customerDataList.get(position).getContactNumber().toString().trim();

        viewHolder.leadMobile.setText(mobileNumber);
        System.out.println("Mobile NUmber about to get registered");

    boolean isChecked = viewHolder.isLeadChecked;

    viewHolder.leadCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                System.out.println("Lead Added to list");
                selectedCustomerList.add(customerDataList.get(position));
                selectedLeadsList.add(leadDataList.get(position));
                System.out.println("Selected Lead List :: " + selectedLeadsList.toString());
                System.out.println("Selected Customer List :: " + selectedCustomerList.toString());
            } else {
                System.out.println("Lead removed from list");
                selectedCustomerList.remove(customerDataList.get(position));
                selectedLeadsList.remove(leadDataList.get(position));
                System.out.println("Selected Lead List :: " + selectedLeadsList.toString());
                System.out.println("Selected Customer List :: " + selectedCustomerList.toString());
            }
        }
    });
}

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

public static class LeadListViewHolder extends RecyclerView.ViewHolder {

    protected TextView leadName;
    protected TextView leadEmail;
    protected TextView leadMobile;
    protected TextView redPopup;
    protected TextView greyPopup;
    protected CheckBox leadCheckBox;
    protected View verticalSeparator;
    protected boolean isLeadChecked;

    public LeadListViewHolder(View v) {
        super(v);

        leadName = (TextView) v.findViewById(R.id.lead_name);

        leadEmail = (TextView) v.findViewById(R.id.lead_email);

        leadMobile = (TextView) v.findViewById(R.id.lead_mobile);

        redPopup = (TextView) v.findViewById(R.id.red_popup);

        greyPopup = (TextView) v.findViewById(R.id.grey_popup);

        leadCheckBox = (CheckBox) v.findViewById(R.id.lead_checkbox);

        verticalSeparator = v.findViewById(R.id.separator);

        mContextAdapter = v.getContext();

        isLeadChecked = false;

        System.out.println("Got Lead Name's Id and handle");

       }
   }
}

共有1个答案

蔚和安
2023-03-14

您正在使用RecycerView。您遇到的功能是回收部分。

列表项被移除并重用--如果在视图中选中了该框,那么在滚动时附加到另一端的新视图也将选中该框,因为在回调中只更改leadcheckbox的状态。

您应该始终使用视图的当前状态初始化视图。如果它被检查了,就检查它,如果它不是,就不要。

 类似资料:
  • 我试图创建一个表单,该表单顶部有一个复选框,当用户选中该复选框时,它会选择其他特定的复选框,但不是所有复选框。我很难通过反复试验或搜索找到答案。我唯一能找到的就是“全选”选项。不是我想要的。 理想情况下,当用户选中“管理包”旁边的复选框时,我想要“Chrome外观组”和“远程启动” 下面是代码和我在这方面的基本尝试,但它不起作用。提前谢谢。 超文本标记语言: Javascript 我不知道这个Ja

  • 我有谷歌表格。用户填写google表单,并创建一个名为“表单响应”的表单来记录用户响应。如果我在W列中打勾,我想使用脚本将数据从“表单响应”表移动到“已验证表单响应”。 表格响应表的链接是 https://docs.google.com/spreadsheets/d/1LUUEZ7sSjBy-WWL3TZt8l6sqtrEBn3dZ03iRDTY1bF0/edit?usp=sharing 我使用的

  • 我创建了一个windows窗体(到目前为止)只包含复选框。构造函数接受一个参数:。对于这个数组中的每个字符串,我都会创建一个复选框。 例如: null 那么,当另一个复选框调用uncheck时,是否有一种方法可以告诉我的复选框不要运行? 下面是我的代码(它都是手工编写的,所以没有使用visual studio设计器):

  • 我正在尝试一个Woocommerce票证插件。默认情况下,该插件会显示一个表,其中包含多个票证变体作为产品,并在下面显示一个文本字段作为其数量和一个提交按钮,以将产品添加到购物车中。 我想用单选按钮或复选框替换这个文本字段,这样用户就可以选择一种类型的票证,然后点击提交按钮将其中的一部分添加到购物车中。 我的想法是在foreach循环中创建一个复选框或单选按钮,该复选框或单选按钮的值为quanti

  • 问题内容: 浏览器仅在提交表单时选中复选框输入值数据,这是浏览器的标准行为吗? 如果未提供任何值数据,默认值是否始终为“ on”? 假设以上内容正确,那么在所有浏览器中这种行为是否一致? 问题答案: 是的,标准行为是仅在选中复选框后才发送值。通常,这意味着您需要记住在服务器端期望使用的复选框的方式,因为并非所有数据都从表单返回。 默认值始终为“ on”,这在所有浏览器中都应保持一致。 W3C HT

  • 我有一个自定义复选框。我想要的是在选中复选框时打开一个引导模式。我想要一个引导模式打开就像点击一个按钮一样。希望你能理解我的问题。我怎么能做到。

  • 选中“第一键”复选框时,我想选中并禁用“第二键”复选框 html代码 为复选框键入脚本代码 } 通过使用此代码,key 2复选框被选中并禁用。但是key 2复选框的布尔值仍然是假的。当我单击key 1复选框时,关于如何将key 2复选框的布尔值更改为true的任何建议我正在使用Angular 8及其反应式形式模块。我是角度新手。

  • 下载整个项目。zip在这里 此问题的代码在Views文件夹中的CreateSchedule.aspx文件中。 以下是GridView的ASP.NET: 在取消选中某个框后调试事件时,网页上的复选框显示为未选中,但的值为true,即使该复选框在网页上显示为未选中。(chkRow是未选中的复选框) 更新3 我取得了一些突破。我发现复选框的问题在于嵌套的gridview位于使用JavaScript折叠和