清除 ListView 中的 RadioGroup
Clearing RadioGroup inside a ListView
我有一个由 ArrayAdapter
设置的自定义 ListView
。
每行有一个 TextView 和 3 个 RadioButtons。
填充 AdapterView
的对象如下所示:
public class MyItem {
public String title = "";
public boolean rb1 = false;
public boolean rb2 = false;
public boolean rb3 = false;
public MyItem(String title, boolean rb1, boolean rb2, boolean rb3) {
this.title = title;
this.rb1 = rb1;
this.rb2 = rb2;
this.rb3 = rb3;
}
}
在 getView()
方法中,我设置了一个 OnLongClickListener
,因为我想在连续长按后清除 selection:
public class MyAdapter extends ArrayAdapter<MyItem> {
private Context context;
private int layoutResourceId;
private ArrayList<MyItem> items = null;
public MyAdapter(Context context, int textViewResourceId, ArrayList<MyItem> objects) {
super(context, textViewResourceId, objects);
this.layoutResourceId = textViewResourceId;
this.context = context;
this.items = objects;
}
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row = convertView;
final MyItemHolder holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MyItemHolder();
holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
holder.rb1 = (RadioButton) row.findViewById(R.id.rb1);
holder.rb2 = (RadioButton) row.findViewById(R.id.rb2);
holder.rb3 = (RadioButton) row.findViewById(R.id.rb3);
row.setTag(holder);
} else {
holder = (MyItemHolder)row.getTag();
}
String title = items.get(position).title;
Boolean rb1Checked = items.get(position).rb1;
Boolean rb2Checked = items.get(position).rb2;
Boolean rb3Checked = items.get(position).rb3;
holder.tvTitle.setText(title);
holder.rb1.setChecked(rb1Checked);
holder.rb2.setChecked(rb2Checked);
holder.rb3.setChecked(rb3Checked);
row.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
items.get(position).rb1 = false;
items.get(position).rb2 = false;
items.get(position).rb3 = false;
holder.rb1.setChecked(false);
holder.rb2.setChecked(false);
holder.rb3.setChecked(false);
notifyDataSetChanged();
return true;
}
});
return row;
}
static class MyItemHolder {
TextView tvTitle;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
}
}
这样就可以了,但是...
比方说,第一个 RadioButton 是 selected,长按该行后,none 个 RadioButtons 是 selected,但我无法 select又是第一个我可以select第二个或第三个,但不是第一个。
您可以使用
解决这个问题
radioGroup.clearCheck()
而不是
radioButtonA.setChecked(false)
为此,您需要为要清除的每个项目获取对单选组的引用。
我有一个由 ArrayAdapter
设置的自定义 ListView
。
每行有一个 TextView 和 3 个 RadioButtons。
填充 AdapterView
的对象如下所示:
public class MyItem {
public String title = "";
public boolean rb1 = false;
public boolean rb2 = false;
public boolean rb3 = false;
public MyItem(String title, boolean rb1, boolean rb2, boolean rb3) {
this.title = title;
this.rb1 = rb1;
this.rb2 = rb2;
this.rb3 = rb3;
}
}
在 getView()
方法中,我设置了一个 OnLongClickListener
,因为我想在连续长按后清除 selection:
public class MyAdapter extends ArrayAdapter<MyItem> {
private Context context;
private int layoutResourceId;
private ArrayList<MyItem> items = null;
public MyAdapter(Context context, int textViewResourceId, ArrayList<MyItem> objects) {
super(context, textViewResourceId, objects);
this.layoutResourceId = textViewResourceId;
this.context = context;
this.items = objects;
}
@Override
public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View row = convertView;
final MyItemHolder holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new MyItemHolder();
holder.tvTitle = (TextView) row.findViewById(R.id.tvTitle);
holder.rb1 = (RadioButton) row.findViewById(R.id.rb1);
holder.rb2 = (RadioButton) row.findViewById(R.id.rb2);
holder.rb3 = (RadioButton) row.findViewById(R.id.rb3);
row.setTag(holder);
} else {
holder = (MyItemHolder)row.getTag();
}
String title = items.get(position).title;
Boolean rb1Checked = items.get(position).rb1;
Boolean rb2Checked = items.get(position).rb2;
Boolean rb3Checked = items.get(position).rb3;
holder.tvTitle.setText(title);
holder.rb1.setChecked(rb1Checked);
holder.rb2.setChecked(rb2Checked);
holder.rb3.setChecked(rb3Checked);
row.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
items.get(position).rb1 = false;
items.get(position).rb2 = false;
items.get(position).rb3 = false;
holder.rb1.setChecked(false);
holder.rb2.setChecked(false);
holder.rb3.setChecked(false);
notifyDataSetChanged();
return true;
}
});
return row;
}
static class MyItemHolder {
TextView tvTitle;
RadioButton rb1;
RadioButton rb2;
RadioButton rb3;
}
}
这样就可以了,但是... 比方说,第一个 RadioButton 是 selected,长按该行后,none 个 RadioButtons 是 selected,但我无法 select又是第一个我可以select第二个或第三个,但不是第一个。
您可以使用
解决这个问题radioGroup.clearCheck()
而不是
radioButtonA.setChecked(false)
为此,您需要为要清除的每个项目获取对单选组的引用。