2016-08-16 6 views
1

私は、各リストアイテムに2つのラジオボタンがあるラジオグループがあるrecyclerviewを持っています。どのようにして各放射線群の状態を正しく保存することができますか?ここに私のコードです。上下スクロールでは、状態は正しくありません。ありがとうrecyclerviewのラジオグループが正しくありません

public class ITOAdapter extends RecyclerView.Adapter<ITOAdapter.ViewHolder> { 
    private Context context; 
    private List<String> list; 
    private List<Model> answers; 
    private Client client; 
    private String test; 

    public ITOAdapter(Context context, List<String> list, Client client, String test) { 
     this.context = context; 
     this.list = list; 
     this.client = client; 
     this.answers = new ArrayList<>(); 
     this.test = test; 
     for (int i=0; i<list.size(); i++) this.answers.add(new Model()); 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     Context context = parent.getContext(); 
     LayoutInflater inflater = LayoutInflater.from(context); 
     View contactView = null; 
     if (viewType == Constants.VIEW_TYPE_CELL) { 
      contactView = inflater.inflate(R.layout.item_ito, parent, false); 
     } 
     else { 
      contactView = inflater.inflate(R.layout.item_ito_footer, parent, false); 
     } 
     return new ViewHolder(contactView); 
    } 

    @Override 
    public int getItemViewType(int position) { 
     return (position == list.size()-1) ? Constants.VIEW_TYPE_FOOTER : Constants.VIEW_TYPE_CELL; 
    } 


    private void setRadio(final ViewHolder holder, int selection) { 

     System.out.println("SELECT:" + selection); 
     RadioButton b1 = holder.rb0; 
     RadioButton b2 = holder.rb1; 

     b1.setChecked(false); 
     b2.setChecked(false); 

     if (selection == 0) b1.setChecked(true); 
     if (selection == 1) b2.setChecked(true); 
    } 


    @Override 
    public void onBindViewHolder(final ViewHolder vh, final int position) { 
     vh.pos = position; 
     vh.number.setText(vh.con + (position+1)); 
     vh.question.setText(list.get(position)); 

     setRadio(vh, answers.get(position).getState()); 

     vh.rb_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup group, int checkedId) { 
       if (checkedId != -1) { 
        RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId); 
        vh.checkId = checkedId; 
        int checkedIndex = group.indexOfChild(checkedRadioButton); 
        answers.get(vh.pos).setState(checkedIndex); 
        setRadio(vh, answers.get(position).getState()); 
       } 
      } 
     }); 
    } 

    @Override 
    public int getItemCount() { 
     return list.size(); 
    } 

    public static class ViewHolder extends RecyclerView.ViewHolder { 
     public TextView number; 
     public TextViewPlus question; 
     public RadioGroup rb_group; 
     public Button btn; 
     public String con; 
     public int checkId; 
     public RadioButton rb0, rb1; 

     public int pos; 

     public ViewHolder(View itemView) { 
      super(itemView); 
      number = (TextView) itemView.findViewById(R.id.number); 
      question = (TextViewPlus) itemView.findViewById(R.id.question); 
      rb0 = (RadioButton) itemView.findViewById(R.id.rb0); 
      rb1 = (RadioButton) itemView.findViewById(R.id.rb1); 
      rb_group = (RadioGroup) itemView.findViewById(R.id.rb_group); 
      btn = (Button) itemView.findViewById(R.id.btn); 
      con = number.getText().toString(); 
     } 
    } 

    private class Model{ 
     private int state; 

     public Model(){ 
      this.state = -1; 
     } 

     public Model(int state){ 
      this.state = state; 
     } 

     public int getState() { 
      return state; 
     } 

     public void setState(int state) { 
      this.state = state; 
     } 
    } 

} 
+0

ラジオボタンの範囲は、リストまたはリスト項目にありますか? –

+0

私があなたを正しく理解していれば、ラジオグループにポジションラジオボタンがあります。例えば、2つのラジオボタンがあり、最初にクリックしてから0を保存します。1 – Samuliak

答えて

0

setRadioをonCheckedChangedから削除します。また、b1.setChecked(false)を削除します。および b2.setChecked(false); setRadioから、私はまだあなたが問題 を取得している知っている参考ListView with RadioGroup in each row

if (checkedId != -1) { 
        RadioButton checkedRadioButton = (RadioButton) group.findViewById(checkedId); 
        vh.checkId = checkedId; 
switch(checkId) 
{ 
case R.id.rb0: 
answers.get(vh.pos).setState(0); 
break; 
case R.id.rb1: 
answers.get(vh.pos).setState(1); 
break; 
} 
} 
+0

私はしましたが、問題が残っています。 – Samuliak

+0

正しいチェック値(0または1)を与えるint checkedIndex = group.indexOfChild(checkedRadioButton)ラジオボタンを2つしか持っていないラジオボタンを子どもにしていると仮定します。私が共有しているリンクは、同様の操作を行うためにスイッチケースを使用しています。 – Ramit

関連する問題