我有一个列表视图
,将列出这是在database.I报警需要添加一个切换
每个列表项旁边的按钮来设置开启/关闭闹钟。
I have a Listview
that will list the alarms which are in the database.I need to add a Toggle
Button beside each list item to set the alarm on/off.
我如何添加切换
按钮在的ListView
?
R.layout.alarm_list
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_reminders"
android:textColor="#FFF"/>
</LinearLayout>
Java的code:
Java Code:
private void fillData() {
Cursor remindersCursor = aDbHelper.fetchAllAlarms();
startManagingCursor(remindersCursor);
// Create an array to specify the fields we want to display in the list
// (only TITLE)
String[] from = new String[] { AlarmDbAdapter.KEY_TITLE };
// and an array of the fields we want to bind those fields to (in this
// case just text1)
int[] to = new int[] { R.id.text1};
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter reminders = new SimpleCursorAdapter(this,
R.layout.alarm_row, remindersCursor, from, to);
setListAdapter(reminders);
}
R.layout.alarm_row
:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:padding="10dip" android:layout_width="242dp"
android:layout_height="wrap_content"/>
我的项目是越来越推迟。
My project is getting delayed.
帮助
有不小的片段答。您的问题。我认为你需要有多种选择。现在,这里有你需要的东西。
There is no small snippet ans. to your problem. I assume you need to have multi-selection. Now here are the things you need.
由于您使用的 SimpleCursorAdapter
,此时应更换与的CursorAdapter
。要做到这一点,你必须扩展它,因为它是一个抽象适配器。一旦你做了,你将覆盖两个功能。
Since you are using SimpleCursorAdapter
, you should replace that with CursorAdapter
. To do so you have to extend it as it is a abstract adapter. Once you done that you will be overriding two functions.
NewView的
您将创建列表项的看法由充气 R.layout.alarm_row
(它应该包含您的切换按钮太)。你必须使开关按钮不点击。
bindView
在这里设置切换按钮和文本的状态,为您的文本视图
newView
Where you will create your list item views by inflating R.layout.alarm_row
(it should contain your toggle button too). You have make toggle button non-clickable.
bindView
where you will set state of toggle button and text for your text view
这是说这就是你需要在活动方。
That said this what you need on the Activity side.
您必须通过让你的ListView多选择模式的Android:choiceMode
XML或使用 setChoiceMode
You have make your ListView to multi-selection mode by android:choiceMode
in xml or using setChoiceMode
.
现在 bindView
将是这样的:
ListView lv = ((ListActivity)context).getListView();
// Containing all check states
SparseBooleanArray sba = lv.getCheckedItemPositions();
// I am using check box
cb.setChecked(false);
// Cursor is passed as an argument.
if(sba != null)
if(sba.get(cursor.getPosition()))
cb.setChecked(true);
裁判文档:
http://developer.android.com/reference/android/widget/ CursorAdapter.html http://developer.android.com/reference/android/widget/ListView.html
http://developer.android.com/reference/android/widget/CursorAdapter.html http://developer.android.com/reference/android/widget/ListView.html