只希望recyclerview中的复选框或单选按钮选中一个复选框

时间:2020-04-15 10:14:56

标签: java android checkbox android-recyclerview

我当前正在使用复选框,但是我也尝试过单选按钮。我似乎一次只能选择一项。如您所见,香港专业教育学院试图实现它,但它不起作用。我希望得到的结果是一次检查一项,即使关闭该应用程序也要保持检查该项目。这是我实现复选框

的地方
        options1 = new FirebaseRecyclerOptions.Builder<contact>().setQuery(databaseReference1,contact.class).build();
        adapter1 = new FirebaseRecyclerAdapter<contact, contactAdapter>(options1) {
            @Override
            protected void onBindViewHolder(@NonNull final contactAdapter holder, final int position, @NonNull contact model) {
                holder.contactName.setText(model.getContactName());
                holder.contactPhone.setText(model.getContactPhone());
                holder.chk.setTag(holder.getAdapterPosition());
                holder.chk.setChecked(position == lastCheckedPos);
                holder.chk.setOnCheckedChangeListener(null);
                if(holder.getAdapterPosition() == lastCheckedPos){
                    holder.chk.setChecked(true);
                }else{
                    holder.chk.setChecked(false);
                }
                holder.chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            holder.chk.setChecked(true);
                        }else {
                            holder.chk.setChecked(false);
                        }
                    }
                });
                holder.chk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for(int i =0; i <adapter1.getItemCount(); i++){
                            if(adapter1.getItem(i).isSelected(true)){
                                number = ((TextView) contactsView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.contactPhone)).getText().toString();
                                Toast.makeText(getActivity(),"Data Inserted....." + number, Toast.LENGTH_SHORT).show();
                            }
                            else{
                                Toast.makeText(getActivity(),"Error", Toast.LENGTH_LONG).show();
                            }
                        }
                    }
                });
            }


我正在使用Firebase RecyclerAdapter从Firebase提取数据。这是我的模型课

public class contact {
    String contactName;
    String contactPhone;
    private boolean isSelected;

    public contact() {
    }

    public contact(String contactName, String contactPhone) {
        this.contactName = contactName;
        this.contactPhone = contactPhone;
    }

    public String getContactName() {
        return contactName;
    }

    public void setContactName(String contactName) {
        this.contactName = contactName;
    }

    public String getContactPhone() {
        return contactPhone;
    }

    public void setContactPhone(String contactPhone) {
        this.contactPhone = contactPhone;
    }

    public boolean isSelected(boolean selected){
        return isSelected= selected;
    }
}

这是我的适配器类:

public class contactAdapter extends RecyclerView.ViewHolder{

    public TextView contactName, contactPhone;
    CheckBox chk;
    AdapterView.OnItemClickListener itemClickListener;



    public contactAdapter(@NonNull View itemView) {
        super(itemView);
        contactName = itemView.findViewById(R.id.contactName);
        contactPhone = itemView.findViewById(R.id.contactPhone);
        chk = itemView.findViewById(R.id.myCheckBox);
    }
}

我尝试了很多事情,但是我看到的所有示例似乎都无济于事。我不知道这是大事还是小事。有任何建议。

1 个答案:

答案 0 :(得分:-1)

存储在共享首选项中选择的单选按钮的文本,并使用notifydatasetchanged()。如果onbind()与sharedpref值不匹配,则将其设置为false