我使用custom listivew
with has buttonGroup
containg 5 radiobuttons
in each row ,
setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int selectedId =viewHolder.radioGroup1.getCheckedRadioButtonId();
}
}
这个方法 selectedId 总是返回相同的值,所以我无法知道选择了哪个单选按钮,请帮助,以下是我的代码,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black">
<com.example.hotelmenu.CustomTextView
android:id="@+id/heading"
android:layout_width="420dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="@android:color/white"
android:layout_marginLeft="20dp"
android:text=""
android:padding="0dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="@+id/radio_group2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:checked="true"
android:textColor="@android:color/white"
android:text="@string/rb1" />
<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="@android:color/white"
android:text="@string/rb2" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="@android:color/white"
android:text="@string/rb3" />
<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="@android:color/white"
android:text="@string/rb4" />
<RadioButton
android:id="@+id/rb5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="@android:color/white"
android:text="@string/rb5" />
</RadioGroup>
<RadioGroup
android:id="@+id/radio_group1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:checked="true"
android:textColor="@android:color/white"
android:text="@string/btnYes" />
<RadioButton
android:id="@+id/rbNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:textColor="@android:color/white"
android:text="@string/btnNo" />
</RadioGroup>
</FrameLayout>
</LinearLayout>
public class Question {
private String name;
private boolean selected;
private String isYesOrNo;
public Question(String name) {
this.name = name;
}
public String getName() {
return name;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public String isYesOrNo() {
return isYesOrNo;
}
public void setYesOrNo(String isYesOrNo) {
this.isYesOrNo = isYesOrNo;
}
@Override
public String toString() {
//String selectedString = selected ? "selected" : "not selected";
String value = isYesOrNo;//? "Yes" : "No";
//return name+" -> "+selectedString+ " with value "+value;
return value;
}
}
import java.util.List;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import java.util.List;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
public class QuestionAdapter extends ArrayAdapter<Question> {
private final List<Question> list;
private final Activity context;
ViewHolder viewHolder;
View convertView;
public QuestionAdapter(Activity context, List<Question> list) {
super(context, R.layout.questionlistitem, list);
this.context = context;
this.list = list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
this.convertView=convertView;
viewHolder = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
convertView = inflator.inflate(R.layout.questionlistitem, null);
viewHolder = new ViewHolder();
viewHolder.text = (com.example.hotelmenu.CustomTextView) convertView.findViewById(R.id.heading);
viewHolder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radio_group1);
viewHolder.radioGroup2 = (RadioGroup) convertView.findViewById(R.id.radio_group2);
viewHolder.radioGroup1.setTag(position);
viewHolder.radioGroup2.setTag(position);
String str=list.get(position).getName();
String strarr[]=str.split(",");
viewHolder.text.setText(strarr[0]);
if(strarr[1].trim().equals("Rating"))
{
list.get(position).setYesOrNo("1");
viewHolder.radioGroup1.setVisibility(View.GONE);
}
else
{
viewHolder.radioGroup2.setVisibility(View.GONE);
list.get(position).setYesOrNo("Yes");
}
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.radioGroup1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// boolean isYesOrNo = (checkedId == R.id.rbYes);
// int getPosition = (Integer) group.getTag();
// Log.d("CheckedId",""+isYesOrNo);
// if(isYesOrNo==true)
// list.get(getPosition).setYesOrNo("Yes");
// else
// list.get(getPosition).setYesOrNo("No");
int getPosition = (Integer) group.getTag();
switch (checkedId) {
case R.id.rbYes:
list.get(getPosition).setYesOrNo("Yes");
break;
case R.id.rbNo:
list.get(getPosition).setYesOrNo("No");
break;
}
// int selectedId =viewHolder.radioGroup1.getCheckedRadioButtonId();
// RadioButton radioSexButton = (RadioButton)viewHolder.radioGroup1.findViewById(selectedId);
// String type=radioSexButton.getText().toString().trim();
//// boolean isYesOrNo = (checkedId == R.id.rb1);
// int getPosition = (Integer) group.getTag();
// Log.d("isYesOrNo", type);
// list.get(getPosition).setYesOrNo(type);
}
});
viewHolder.radioGroup2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// int selectedId =viewHolder.radioGroup2.getCheckedRadioButtonId();
// RadioButton radioSexButton = (RadioButton)viewHolder.radioGroup2.findViewById(selectedId);
// String type=radioSexButton.getText().toString().trim();
//// boolean isYesOrNo = (checkedId == R.id.rb1);
int getPosition = (Integer) group.getTag();
// Log.d("isYesOrNo", type);
// list.get(getPosition).setYesOrNo(type);
switch (checkedId) {
case R.id.rb1:
Log.d("CheckedId","1");
list.get(getPosition).setYesOrNo("1");
break;
case R.id.rb2:
Log.d("CheckedId","2");
list.get(getPosition).setYesOrNo("2");
break;
case R.id.rb3:
Log.d("CheckedId","3");
list.get(getPosition).setYesOrNo("3");
break;
case R.id.rb4:
Log.d("CheckedId","4");
list.get(getPosition).setYesOrNo("4");
break;
case R.id.rb5:
Log.d("CheckedId","5");
list.get(getPosition).setYesOrNo("5");
break;
}
}
});
return convertView;
}
}