Reputation: 45921
I'm developing an Android application with a ListView
. Every item in this ListView
will have a checkbox
.
I'm trying to uncheck every item when user clicks on one of them. E.g. a ListView
with two items, 0, 1.
At this moment 0 is selected and user taps on item 1. I want to deselect item 0.
This is my code. It's inside an ArrayAdapter
.
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(itemLayoutId, parent, false);
}
Gate gate = gates.get(position);
if (gate != null)
{
TextView itemText = (TextView)row.findViewById(R.id.gateNameText);
if (itemText != null)
itemText.setText(gate.getName());
ImageView imageView =
(ImageView)row.findViewById(R.id.gateTypeImage);
int resId;
switch (gate.getSelTypeOperation())
{
case 0:
resId = R.drawable.garage;
break;
case 1:
resId = R.drawable.pulse;
break;
case 2:
resId = R.drawable.onoff;
break;
default:
resId = R.drawable.garage;
break;
}
imageView.setImageResource(resId);
CheckBox checkBox =
(CheckBox)row.findViewById(R.id.gateSelectedCheck);
if (checkBox != null)
{
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
// Solo hago algo si el nuevo estado es marcado.
if (isChecked)
for(int i = 0; i < gates.size(); i++)
{
if (i != position)
{
Log.v("GatesAdapter", "uncheck: " + position);
View view = getView(i, null, null);
CheckBox checkBox =
(CheckBox)view.findViewById(R.id.gateSelectedCheck);
checkBox.setChecked(false);
}
}
}
});
checkBox.setChecked(selectedGateIndex == position);
}
}
return row;
}
But, it doesn't work.
What am I doing wrong?
Upvotes: 0
Views: 12030
Reputation: 6836
Just change your getview()
method as shown below:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final Holder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.raw_customelabel_list, null);
holder = new Holder();
holder.txtname = (TextView) vi
.findViewById(R.id.raw_customlabel_txt_name);
holder.checkbox = (CheckBox) vi
.findViewById(R.id.raw_customlabel_checkbox);
vi.setTag(holder);
} else {
holder = (Holder) vi.getTag();
}
vi.setTag(holder);
holder.txtname.setText(strarry[position]);
holder.checkbox.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < strarry.length; i++) {
if (v.getId() == i) {
checkarry[i] = true;
Log.v("check", ""+position);
} else {
checkarry[i] = false;
}
}
notifyDataSetChanged();
}
});
if (checkarry[position]) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
return vi;
}
}
Upvotes: 1
Reputation: 8042
Use these below code it will work fine for you
int selected_position =-1
public View getView(final int position, View convertView, ViewGroup parent)
{
if(selected_position==position)
{
CHECKBOX.setChecked(true);
}
else
{
CHECKBOX.setChecked(false);
}
CHECKBOX.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (((CheckBox) view).isChecked())
{
selected_position= position;
}
else
{
selected_position=-1;
}
notifyDataSetChanged();
}
});
return view;
}
}
Upvotes: 1
Reputation: 6338
Use radio buttons. If I see check boxes, I think I may select as many as I want.
Is not solving your issue if you just replace your CheckBox object with a RadioButton in your code?
Upvotes: 0
Reputation: 1975
You need to use listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) and, u need to make your custom view's row implement checkable. . Another SO question already - soln is here
Upvotes: 1