我正在开发一个应用程序,其中包含参加不同 Activity 的成员列表。
参加一组游戏的一群人。但是一个人只能参加一场比赛。所以我选择了 RadioButton
。四个游戏,即 A、B、C 和 D。People 列表中列出了一个包含四个 RadioButton
的列表,我们需要为每个人从中选择一个。
我已经尝试使用自定义行数据的 GridView
如下...
<GridView
android:id="@+id/gridViewMarkList"
android:layout_width="match_parent"
android:layout_height="326dp"
android:numColumns="1" >
</GridView>
我的网格列表是单列,在 row.xml 中有以下数据
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="fill_parent"
android:padding="10dp" >
<TableRow>
<TextView
android:id="@+id/SNo"
style="@style/text"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Name"
style="@style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/A"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/B"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/C"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/D"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
</TableRow>
现在我无法检索 Activity 中的数据..
给我任何执行此过程的建议......!
最佳答案
对于这种情况,您将需要一个自定义 ListView。 使您的代码如下所示以实现它..
行.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout>
<TextView
android:id="@+id/SNo"
style="@style/text"
android:layout_width="100dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Name"
style="@style/text"
android:layout_width="180dp"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/A"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="@+id/B"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/C"
android:layout_width="40dp"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/D"
android:layout_width="40dp"
android:layout_height="wrap_content" />
</RadioGroup>
CustomListAdapter.java
public class CustomListAdapter extends ArrayAdapter<String> {
private Context mContext = null;
private int id;
private List<String> list = null;
public CustomListAdapter(Context context, int textViewResourceId, List<String> list) {
super(context, textViewResourceId, patientNameList);
mContext = context;
id = textViewResourceId;
this.list = list;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;
if (mView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mView = inflater.inflate(id, null);
}
/*
here you go with your views which you gonna display
ex. : RadioButton gameOption = (RadioButton) mView.findViewById(R.id.gameOptionRadioBtn);
*/
return mView;
}
}
InYourActivityClass
ArrayAdapter<String> adapter = new CustomListAdapter(this, R.layout.row, list);
listView.setAdapter(adapter);
希望这会有所帮助:)
关于android - 在android中列出多个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15629868/