-2
我正在使用一個片段佈局來顯示ListView中的一行,並且我使用ListAdapter來填充ListView。每行包含一個TextView和一個RadioGroup,每個Radio組包含2個RadioButtons,用於yes和no。Android - 在組中選擇一個單選按鈕導致另一個組中的另一個單選按鈕被選中
問題是,當我運行該應用程序時,當我在第一組上選擇一個RadioButton(如yes)時,它會導致在ListView的另一行中的另一個組中選擇另一個RadioButton(也是)。
這是該行片段佈局代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/questionTextView">
<TextView
android:text="Dis one na question wey go need yes or no answer?"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/questionView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="19dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="20sp" />
<RadioGroup
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/customRadioGroup"
android:layout_marginTop="16dp"
android:layout_below="@+id/questionView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:text="Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yesRadioBtn"
android:layout_alignBaseline="@+id/noRadioBtn"
android:layout_alignBottom="@+id/noRadioBtn"
android:layout_toLeftOf="@+id/noRadioBtn"
android:layout_toStartOf="@+id/noRadioBtn"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp" />
<RadioButton
android:text="No"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/noRadioBtn"
android:layout_below="@+id/questionsTitle"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp" />
</RadioGroup>
而且我ListAdapter類:
public class MyCustomAdapter extends BaseAdapter implements ListAdapter {
private ArrayList<String> list = new ArrayList<String>();
private Context context;
private String[] answers;
public MyCustomAdapter(ArrayList<String> list, Context context) {
this.list = list;
this.context = context;
this.answers = new String[list.size()];
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int pos) {
return list.get(pos);
}
@Override
public long getItemId(int pos) {
//return list.get(pos).getId();
//just return 0 if your list items do not have an Id variable.
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.question_row, null);
}
//Handle TextView and display string from your list
TextView listItemText = (TextView)view.findViewById(R.id.questionView);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
RadioGroup radioGroup = (RadioGroup)view.findViewById(R.id.customRadioGroup);
//radioGroup.setId(position);
View vYes = radioGroup.getChildAt(0);
View vNo = radioGroup.getChildAt(1);
RadioButton yesRadioBtn = (RadioButton)vYes;//(RadioButton)view.findViewById(R.id.yesRadioBtn);
RadioButton noRadioBtn = (RadioButton)vNo;//(RadioButton)view.findViewById(R.id.noRadioBtn);
yesRadioBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
answers[position] = "yes";
notifyDataSetChanged();
}
});
noRadioBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
answers[position] = "no";
notifyDataSetChanged();
}
});
return view;
}
public String[] getAnswers()
{
return this.answers;
}
}