2017-11-25 3 views
0

Ich habe eine Recyclerview mit zwei Radiobuttons in jeder Reihe. Einer ist für "Ja" und ein anderer für "Nein". Ich möchte nur eine Optionsschaltfläche aus jeder Zeile gleichzeitig auswählen und den Wert oder die Position dieser Optionsschaltfläche in einer Array-Liste abrufen. Um eine Einzelauswahl zu erreichen, habe ich diese beiden Radiobuttons in eine Radiogruppe eingefügt. Ich muss das Optionsfeld "Nein" in jeder Zeile standardmäßig aktivieren. Später kann der Benutzer entweder 'Ja' oder 'Nein' auswählen und die ausgewählten Werte sollten in einer ArrayList gespeichert werden. Wie erreiche ich das?Recyclerview mit Radio Gruppe wählen Sie nur ja oder nein

Hier ist mein Adaptercode.

public class MyHealthInfoAdapter extends RecyclerView.Adapter<MyHealthInfoAdapter.MyHealthViewHolder> { 

private Context context; 
private ArrayList<DonorHealthStatusQuestionare> listQuestions; 
private RadioButton lastCheckedRadioBtn = null; 

private ArrayList<Integer> listYes = new ArrayList<>(); 
private ArrayList<Integer> listNo = new ArrayList<>(); 

private FloatingActionButton floatingActionButton; 


public MyHealthInfoAdapter() { 
} 

public MyHealthInfoAdapter(Context context, ArrayList<DonorHealthStatusQuestionare> listQuestions) { 
    this.context = context; 
    this.listQuestions = listQuestions; 
} 

@Override 
public MyHealthViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_my_health_info, parent, false); 
    return new MyHealthViewHolder(view); 
} 

@Override 
public void onBindViewHolder(final MyHealthViewHolder holder, final int position) { 

    final DonorHealthStatusQuestionare donorHealthStatusQuestionare = listQuestions.get(position); 
    holder.tvQuestion.setText(donorHealthStatusQuestionare.getQuestions()); 

} 

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

public class MyHealthViewHolder extends RecyclerView.ViewHolder { 

    CardView cvParent; 
    TextView tvQuestion; 
    RadioButton radioYes, radioNo; 
    RadioGroup radioGroup; 

    public MyHealthViewHolder(View itemView) { 
     super(itemView); 
     cvParent = itemView.findViewById(R.id.card_view_my_health_status_rv); 
     tvQuestion = itemView.findViewById(R.id.tv_eligibility_question_my_info); 
     radioYes = itemView.findViewById(R.id.radio_yes_my_health_info); 
     radioNo = itemView.findViewById(R.id.radio_no_my_health_info); 

     radioGroup = itemView.findViewById(R.id.my_health_status_check_group); 


    } 

} 


} 

Und hier ist meine Recyclerview Layout.

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/card_view_my_health_status_rv" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_marginBottom="1dp"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:padding="12dp"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_marginEnd="16dp" 
     android:layout_marginRight="16dp" 
     android:layout_weight="1"> 

     <TextView 
      android:id="@+id/tv_eligibility_question_my_info" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

    </RelativeLayout> 


    <RadioGroup 
     android:orientation="horizontal" 
     android:id="@+id/my_health_status_check_group" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"> 

     <RadioButton 
      android:id="@+id/radio_yes_my_health_info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginEnd="16dp" 
      android:layout_marginRight="16dp" 
      android:text="@string/yes" /> 

     <RadioButton 
      android:id="@+id/radio_no_my_health_info" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/no" /> 

    </RadioGroup> 


</LinearLayout> 

Antwort

0
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
       @Override 
       public void onCheckedChanged(RadioGroup group, int checkedId) { 
       RadioButton rbbutton=(RadioButton)findViewByID(checkedId); 
       if(rbbutton.getText().toString().equalsIgnoreCase("Yes")){ 
       listYes.add(position) 
       }else{ 
        listNo.add(position); 
       } 
       } 
      }); 

Code Snippet für Make alle deaktiviert:

for(int i=0;i<listQuestions.size();i++){ 
//do your logic here. 
} 
+0

hieraus Wo checkedId kommen. –

+0

CheckID ist die ID der ausgewählten Radiobutton-ID. Wenn Sie es verwenden, können Sie das gleiche Optionsfeld erstellen und auf alle Eigenschaften dieses Optionsfelds zugreifen. –

+0

Ok, hab es. So legen Sie alle No als standardmäßig ausgewählt fest. –