【发布时间】:2014-02-16 07:57:59
【问题描述】:
我正在开发一个应用程序,我需要在其中实现列表视图中的单选按钮。我想实现一个列表视图,每行有一个单选按钮和两个文本视图。以及列表视图下方的一个“确定”按钮。
我所做的是创建了一个列表视图和一个自定义适配器。 listview的代码如下:
<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>
fragment的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;
}
}
textview1 的数据是从 SQLite 数据库填充的,而在 textview2 上的数据是“状态关闭”。在选择或单击任何单选按钮时,文本视图的文本将更改为“状态打开”。
问题是: 应用程序的需要是应该只选择一个单选按钮,并且 textview2 的数据在选择时会发生变化。当用户单击另一个单选按钮时,它会被选中,而前一个单选按钮应该被取消选择,textview2 的文本会更改为先前选择的单选按钮的“状态关闭”,然后单击单选按钮以“状态打开”。
编辑 1:
然后单击“确定”按钮,我想获取列表视图 textview1 和 textview2 的位置、文本,因为我想将该文本保存在审查中的 SQLite 数据库中。
请指导我应该遵循哪些步骤。我正在申请中。需要您的宝贵指导。
【问题讨论】:
标签: android android-layout android-listview radio-button android-adapter