清除ListView中的RadioGroup

时间:2017-12-22 09:25:54

标签: android listview android-radiobutton

我有ListView设置的自定义ArrayAdapter。 每行都有一个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,因为我想在长按一行后清除选择:

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被选中,在长按行后,没有选择任何RadioButton,但我无法再选择第一个。我可以选择第二个或第三个,但不能选择第一个。

1 个答案:

答案 0 :(得分:1)

您可以使用

解决此问题
radioGroup.clearCheck()

而不是

radioButtonA.setChecked(false)

为此,您需要获取要清除的每个项目的无线电组的引用。