安卓:自定义列表视图单选按钮 [英] Android: Radio button in custom list view
问题描述
我正在开发中,我需要实现在列表视图中单选按钮的应用程序。我想实现有一个单选按钮,并在每一行中两个文本视图列表视图。和一个OK按钮下面的列表视图。
I am developing an application in which I need to implement radio buttons in list view. I want to implement a list view having one radio button and two text view in each row. And one button "Ok" below listview.
我所做的就是我创建的列表视图和自定义适配器。 列表视图的code是因为:
What I have done is I created a list view and a custom adapter. The code of listview is as:
<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>
和我创建了一个自定义适配器布局:
And I created a custom adapter layout as:
<?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 code是如下:
The java code of fragment is as follows:
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) {}
和适配器的code是如下:
And the code of adapter is as follows:
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 rowView;
}
}
textview1的数据是从SQLite数据库和填充textview2的数据是状态已经关闭。而在选择或点击任何单选按钮的文本视图的文本得到改为开放状态。
The data of textview1 are populated from SQLite database and on textview2 the data are "Status Closed". And on selection of or by clicking any radio button the text of text view get changed to "Status Open".
的问题是:应用程序的需求是,只有一个单选按钮应该选择和数据textview2得到改变的选择。而当另一单选按钮,用户点击它得到选择和previous应该得到取消和textview2的文字得到更改为状态已经关闭pviously选定的单选按钮$ P $,并点击单选按钮状态开
The Issue is: The need of application is that only one radio button should get select and data of textview2 get change on selection. And when user click on the other radio button it get select and the previous one should get deselect and text of textview2 get changed to "Status Closed" of a previously selected radio button and clicked radio button to "Status Open".
修改1:
和的onclick确定按钮,我想要得到的位置,列表视图textview1和textview2文字,因为我想保存在SQLite数据库中deview该文本。
And onclick on "OK" button i want to get the position, text of list view textview1 and textview2, as i want to save that text in SQLite database in deview.
请指引我,我应该遵循什么样的步骤。我在我的应用程序的中间。您宝贵的指导意见是必需的。
Please guide me what steps should I follow. I am in middle of my application. Your valuable guidance is required.
推荐答案
下面是主要的想法
- 当
单选
被选中,我们必须调用notifyDataSetChanged()
,以便所有的观点得到更新。 - 当
单选
被选中,我们必须建立一个selectedPosition
,跟踪其中单选按钮
选择 -
查看
s的循环里的ListView
秒。因此,在ListView控件的绝对位置的变化
。因此,在ListAdapter#getView()
,我们必须调用setTag()
每个单选按钮
。这使我们能够确定在列表中的单选
的当前位置时,单选
点击。 -
单选#setChecked()
必须在getView更新()
新建或pre存在查看
秒。
- when a
RadioButton
is checked we must callnotifyDataSetChanged()
, so that all views get updated. - when a
RadioButton
is checked we must set aselectedPosition
, to keep track of whichRadioButton
is selected View
s are recycled insideListView
s. Therefore, their absolute position changes in theListView
. Therefore, insideListAdapter#getView()
, we must callsetTag()
on eachRadioButton
. This allows us to determine the current position of theRadioButton
in the list when theRadioButton
is clicked.RadioButton#setChecked()
must be updated insidegetView()
for new or pre-existingView
s.
下面是一个例子ArrayAdapter我写的,为了证明这些想法进行测试
Here is an example ArrayAdapter I wrote and tested in order to demonstrate these ideas
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// I do no use these values anywhere inside the ArrayAdapter. I could, but don't.
final Integer[] values = new Integer[] {1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,};
ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, R.layout.row, R.id.textview, values) {
int selectedPosition = 0;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
}
TextView tv = (TextView)v.findViewById(R.id.textview);
tv.setText("Text view #" + position);
RadioButton r = (RadioButton)v.findViewById(R.id.radiobutton);
r.setChecked(position == selectedPosition);
r.setTag(position);
r.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
selectedPosition = (Integer)view.getTag();
notifyDataSetChanged();
}
});
return v;
}
};
setListAdapter(adapter);
}
}
这篇关于安卓:自定义列表视图单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!