My app shows a ListView
with a different number of CheckBoxes
for each row
. The maxium number of CheckBoxes
for a row is three. My app looks like this:
- If the left
CheckBox
gets checked, allCheckBoxes
on the right side should get checked automatically. - If the right
CheckBox
gets unchecked, allCheckBoxes
on the left side should get unchecked automatically.
I´ll give a little example here:
There are three CheckBoxes (cb1, cb2 and cb3)
in Row one. cb1
is on the left side, cb2
in the middle and cb3
on the right side.
- All
CheckBoxes
are currently unchecked. The user checks the left one (cb1
). All otherCheckBoxes (cb2 and cb3)
are getting automatically checked. - All
CheckBoxes
are currently checked. The user unchecks the right one (cb3
). All otherCheckBoxes (cb1 and cb2)
are getting automatically unckecked.
I tried to solve my problem with OnCheckedChangeListener
, but the following message occurs
"Variable 'Holder' is accessed from within the inner class, needs to be declared final."
My question:
-
How to fix this, so that Message doesn´t occur?
-
Is my solution logically right?
If my problem is not clear, pls leave a comment.
My solution:
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
}
}
});
Custom Adapter:
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;
}
}
Row Layout:
<?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>
Aucun commentaire:
Enregistrer un commentaire