我的应用显示了一个ListView
,每个CheckBoxes
的{{1}}的数量不同。连续row
的最大数量为三。我的应用程序如下所示:
CheckBoxes
被选中,则右侧的所有CheckBox
应该被自动选中。CheckBoxes
,则左侧的所有CheckBox
都将自动处于未选中状态。 我在这里举一个例子:
第一行中有三个CheckBoxes
。 CheckBoxes (cb1, cb2 and cb3)
位于左侧,cb1
位于中间,cb2
位于右侧。
cb3
都未选中。用户检查左一个(CheckBoxes
)。所有其他cb1
都会被自动检查。CheckBoxes (cb2 and cb3)
。用户取消选中右边的(CheckBoxes
)。
所有其他cb3
都会自动取消锁定。 我尝试用CheckBoxes (cb1 and cb2)
解决我的问题,但出现以下消息
“变量'Holder'是从内部类内部访问的,需要 被宣布为最终裁决。”
我的问题:
如何解决此问题,以便不会出现消息?
我的解决方案在逻辑上正确吗?
如果我的问题不清楚,请发表评论。
我的解决方案:
OnCheckedChangeListener
自定义适配器:
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;
}
}
答案 0 :(得分:1)
创建一个引用您final
的{{1}}变量,并在onCheckedChangeListener中使用它:
ViewHolder
对cb3
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 } } });
也执行相同操作。