2017-12-22 7 views
0

ArrayAdapterで設定しました。 各行にはTextViewと3つのラジオボタンがあります。 AdapterView次のようになり移入ListView内のRadioGroupをクリアする

オブジェクト:getView()メソッド内

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; 
    } 

} 

は、私は、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は、行の長押し後、ラジオボタンのどれが選択されていない、選択されたとしましょう、しかし、私は最初に選択することができませんよもう一度。私は2番目または3番目のものを選択できますが、最初のものは選択できません。

+0

'your_radiobutton1.setChecked(false);'を使用して、消去したい特定の項目の中のすべてのRadioButtonに対してこれを試してください。 – dalla92

+0

私はそれを試みましたが、それは同じように動作します。 – Dusan

+1

アダプタコード全体を更新 – ABDevelopers

答えて

1

あなたがこれを行うために

radioGroup.clearCheck() 

代わりの

radioButtonA.setChecked(false) 

を使用することで、この問題を解決することができ、あなたがクリアしたいすべての項目のラジオグループへの参照を取得する必要があります。

+0

はい!ありがとうございました。 – Dusan

関連する問題