我正在开发一个应用程序,需要在列表视图中实现单选按钮。我想要实现一个列表视图,每行有一个单选按钮和两个文本视图。并且在列表视图下方有一个“确定”按钮。
我已经创建了一个列表视图和一个自定义适配器。 列表视图的代码如下:
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:overScrollMode="never"
tools:ignore="NestedScrolling"
android:choiceMode="singleChoice" >
</ListView>
我创建了一个自定义适配器布局:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:ignore="UselessParent" >
<RadioButton
android:id="@+id/radiobutton"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight=".1" />
<TextView
android:id="@+id/textview1"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight=".3" />
<TextView
android:id="@+id/textview2"
android:layout_width="0sp"
android:layout_height="wrap_content"
android:layout_weight=".3" />
</TableRow>
</TableLayout>
片段的Java代码如下:
ListView listView = (ListView) view.findViewById(R.id.listview);
// values is a StringArray holding some string values.
CustomAdapter customAdapter = new CustomAdapter (getActivity(), values);
listView.setAdapter(customAdapter );
listView.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {}
适配器的代码如下:
public class CustomAdapter extends ArrayAdapter<String> {
/** Global declaration of variables. As there scope lies in whole class. */
private Context context;
private String[] listOfValues;
/** Constructor Class */
public CustomAdapter (Context c,String[] values) {
super(c,R.layout.adapter_layout,values);
this.context = c;
this.listOfValues = values;
}
/** Implement getView method for customizing row of list view. */
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
// Creating a view of row.
View rowView = inflater.inflate(R.layout.adapter_layout, parent, false);
TextView textView1 = (TextView)rowView.findViewById(R.id.textview1);
TextView textView2 = (TextView)rowView.findViewById(R.id.textview2);
RadioButton radioButton = (RadioButton) rowView.findViewById(R.id.radiobutton);
radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, CustomAdapter[position], Toast.LENGTH_SHORT).show();
}
});
return review;
}
}
文本视图1的数据来自SQLite数据库,文本视图2的数据为“状态关闭”。当选择某个单选按钮或点击它时,文本视图的文本将更改为“状态打开”。
问题是:应用程序的需求是只能选择一个单选按钮,选择后文本视图2的数据会更改。当用户点击其他单选按钮时,前一个单选按钮应该取消选择,并且先前选择的单选按钮的文本视图2的文本应更改为“状态关闭”,而点击的单选按钮的文本视图2的文本应更改为“状态打开”。
编辑1:
并且在单击“确定”按钮后,我希望获取列表视图文本视图1和文本视图2的位置和文本,因为我想将该文本保存在审核中的SQLite数据库中。
请指导我应该按照什么步骤执行。我正在我的应用程序中间。需要您宝贵的指导。