gpt4 book ai didi

java - Android:检查一个复选框会检查同一行中的所有复选框

转载 作者:行者123 更新时间:2023-12-02 10:45:47 25 4
gpt4 key购买 nike

我的应用程序显示 ListView具有不同数量的CheckBoxes对于每个 row 。最大数量CheckBoxes一行是三。我的应用程序如下所示:

enter image description here

  1. 如果左边CheckBox全部被检查CheckBoxes右侧应该会自动检查。
  2. 如果正确CheckBox全部未经检查CheckBoxes左侧应该会自动取消选中。

我在这里举一个小例子:
有三个CheckBoxes (cb1, cb2 and cb3)在第一行。 cb1在左侧,cb2在中间和 cb3在右侧。

  1. 全部 CheckBoxes目前尚未检查。用户检查左侧( cb1 )。所有其他CheckBoxes (cb2 and cb3)会被自动检查。
  2. 全部 CheckBoxes目前正在检查中。用户取消选中右侧的一项 ( cb3 )。所有其他CheckBoxes (cb1 and cb2)会自动取消选中。

我尝试用 OnCheckedChangeListener 解决我的问题,但出现以下消息

"Variable 'Holder' is accessed from within the inner class, needs to be declared final."

我的问题:

  1. 如何解决这个问题,以免出现消息?

  2. 我的解决方案在逻辑上正确吗?

如果我的问题不清楚,请发表评论。

我的解决方案:

 holder.cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case1: Left (cb1) gets checked -> set all other right CB´s (cb2 & cb3) checked
if(isChecked){
holder.cb2.setChecked(isChecked); //Error Message occurs here
holder.cb3.setChecked(isChecked); //Error Message occurs here
}
}
});

holder.cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case2: Middle (cb2) gets checked -> set right CB (cb3) checked
if(isChecked){
holder.cb3.setChecked(isChecked); //Error Message occurs here
}
//Case3: Middle (cb2) gets unchecked -> set left CB (cb1) unchecked
else {
holder.cb1.setChecked(isChecked); //Error Message occurs here
}
}
});

holder.cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case4: Right (cb3) gets unchecked -> set all other left CB´s (cb1 & cb 2) unchecked
if(!isChecked){
holder.cb2.setChecked(isChecked); //Error Message occurs here
holder.cb1.setChecked(isChecked); //Error Message occurs here
}
}
});

自定义适配器:

public class ItemListAdapter extends BaseAdapter {

private LayoutInflater inflater;
private ArrayList<Object> itemArray;
int resIdImage;
private static final int TYPE_ListElement = 0;
private static final int TYPE_DIVIDER = 1;

public ItemListAdapter(Context context, ArrayList<Object> itemArray, int resource) {
this.itemArray = itemArray;
this.resIdImage = resource;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return itemArray.size();
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public Object getItem(int position) {
return itemArray.get(position);
}

@Override
public int getViewTypeCount() {
// TYPE_PERSON and TYPE_DIVIDER
return 2;
}

@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof ItemList) {
return TYPE_ListElement;
}

return TYPE_DIVIDER;
}

@Override
public boolean isEnabled(int position) {
return (getItemViewType(position) == TYPE_ListElement);
}

public class DataHolder{
ImageView imageView;
TextView textView;
CheckBox cb1;
CheckBox cb2;
CheckBox cb3;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {

DataHolder holder = null;
int type = getItemViewType(position);
if (convertView == null) {
switch (type) {
case TYPE_ListElement:
convertView = inflater.inflate(resIdImage, parent, false);
holder = new DataHolder();
holder.imageView = (ImageView) convertView.findViewById(R.id.image);
holder.textView = (TextView) convertView.findViewById(R.id.nameLabel);
holder.cb1 = (CheckBox) convertView.findViewById(R.id.checkbox);
holder.cb2 = (CheckBox) convertView.findViewById(R.id.checkbox2);
holder.cb3 = (CheckBox) convertView.findViewById(R.id.checkbox3);
convertView.setTag(holder);
break;
case TYPE_DIVIDER:
convertView = inflater.inflate(R.layout.row_header, parent, false);
break;
}
}
else {
holder = (DataHolder)convertView.getTag();
}

switch (type) {
case TYPE_ListElement:
ItemList item = (ItemList) getItem(position);
holder.textView.setText(item.getTitle());
holder.imageView.setImageResource(item.resIdImage);

int numCheckBox = item.numCheckBox;
if(numCheckBox == 1){
holder.cb1.setVisibility(View.VISIBLE);
holder.cb2.setVisibility(View.GONE);
holder.cb3.setVisibility(View.GONE);
} else if (numCheckBox == 2){
holder.cb1.setVisibility(View.VISIBLE);
holder.cb2.setVisibility(View.VISIBLE);
holder.cb3.setVisibility(View.GONE);
} else if (numCheckBox == 3){
holder.cb1.setVisibility(View.VISIBLE);
holder.cb2.setVisibility(View.VISIBLE);
holder.cb3.setVisibility(View.VISIBLE);
}
holder.cb1.setChecked(item.checked);
holder.cb2.setChecked(item.checked);
holder.cb3.setChecked(item.checked);

holder.cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case1: Left (cb1) gets checked -> set all other right CB´s (cb2 & cb3) checked
if(isChecked){
holder.cb2.setChecked(isChecked); //Error Message occurs here
holder.cb3.setChecked(isChecked); //Error Message occurs here
}
}
});

holder.cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case2: Middle (cb2) gets checked -> set right CB (cb3) checked
if(isChecked){
holder.cb3.setChecked(isChecked); //Error Message occurs here
}
//Case3: Middle (cb2) gets unchecked -> set left CB (cb1) unchecked
else {
holder.cb1.setChecked(isChecked); //Error Message occurs here
}
}
});

holder.cb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case4: Right (cb3) gets unchecked -> set all other left CB´s (cb1 & cb 2) unchecked
if(!isChecked){
holder.cb2.setChecked(isChecked); //Error Message occurs here
holder.cb1.setChecked(isChecked); //Error Message occurs here
}
}
});

break;

case TYPE_DIVIDER:
TextView title = (TextView)convertView.findViewById(R.id.headerTitle);
String titleString = (String)getItem(position);
title.setText(titleString);
break;
}


return convertView;
}
}

行布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher"
android:layout_gravity="center"
android:layout_margin="20dp"/>

<TextView
android:id="@+id/nameLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_margin="20dp"
android:text="Name"
android:layout_weight="1"
/>

<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="right|center"
android:gravity="right|center"
/>

<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="right|center"
android:gravity="right|center"
/>

<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_gravity="right|center"
android:gravity="right|center"
/>

</LinearLayout>

最佳答案

创建一个引用您的ViewHolderfinal变量,并在onCheckedChangeListener中使用它:

final DataHolder finalHolder = holder;

holder.cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case1: Left (cb1) gets checked -> set all other right CB´s (cb2 & cb3) checked
if(isChecked){
finalHolder.cb2.setChecked(isChecked); // NO MORE ERROR
finalHolder.cb3.setChecked(isChecked); //NO MORE ERROR
}
}
});

holder.cb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
buttonView.setChecked(isChecked);
//Case2: Middle (cb2) gets checked -> set right CB (cb3) checked
if(isChecked){
finalHolder.cb3.setChecked(isChecked); // NO MORE ERROR
}
//Case3: Middle (cb2) gets unchecked -> set left CB (cb1) unchecked
else {
finalHolder.cb1.setChecked(isChecked); //NO MORE ERROR
}
}
});

Do the same for cb3 onCheckedChangeListener as well.

关于java - Android:检查一个复选框会检查同一行中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52596746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com