Android 带有自定义列表的AutoCompleteTextView:如何设置侦听器
我正在开发一个使用标签的应用程序。访问这些内容应该尽可能简单。使用AutoCompleteTextView对我来说似乎很合适。我想要的是:Android 带有自定义列表的AutoCompleteTextView:如何设置侦听器,android,xml,custom-controls,autocompletetextview,android-cursor,Android,Xml,Custom Controls,Autocompletetextview,Android Cursor,我正在开发一个使用标签的应用程序。访问这些内容应该尽可能简单。使用AutoCompleteTextView对我来说似乎很合适。我想要的是: 现有标签应显示在可选列表中,并在每个项目的一侧显示一个复选框 现有标记应显示在AutoCompleteTextView的焦点上(即,在键入字母后不显示) 到目前为止,我所做的是将标记存储在专用的sqlite3表中。查询标记会生成一个光标。光标被传递到SimpleCorsorAdapter,如下所示: Cursor cursor = dbHelper.ge
- 现有标签应显示在可选列表中,并在每个项目的一侧显示一个复选框
- 现有标记应显示在AutoCompleteTextView的焦点上(即,在键入字母后不显示)
Cursor cursor = dbHelper.getAllTags();
startManagingCursor(cursor);
String[] columns = new String[] { TagsDB._TAG};
int[] to = new int[] { R.id.tv_tags};
SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter(this, R.layout.tags_row, cursor, columns, to);
actv.setAdapter(cursAdapt);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="4dip" android:paddingRight="4dip"
android:orientation="horizontal">
<TextView android:id="@+id/tv_tags" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000" android:onClick="actv_item_click" />
<CheckBox android:id="@+id/cb_tags" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="actv_item_checked" />
</LinearLayout>
如您所见,我创建了标记\u row.xml,如下所示:
Cursor cursor = dbHelper.getAllTags();
startManagingCursor(cursor);
String[] columns = new String[] { TagsDB._TAG};
int[] to = new int[] { R.id.tv_tags};
SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter(this, R.layout.tags_row, cursor, columns, to);
actv.setAdapter(cursAdapt);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="4dip" android:paddingRight="4dip"
android:orientation="horizontal">
<TextView android:id="@+id/tv_tags" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000" android:onClick="actv_item_click" />
<CheckBox android:id="@+id/cb_tags" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="actv_item_checked" />
</LinearLayout>
看起来是这样的:
Cursor cursor = dbHelper.getAllTags();
startManagingCursor(cursor);
String[] columns = new String[] { TagsDB._TAG};
int[] to = new int[] { R.id.tv_tags};
SimpleCursorAdapter cursAdapt = new SimpleCursorAdapter(this, R.layout.tags_row, cursor, columns, to);
actv.setAdapter(cursAdapt);
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:paddingLeft="4dip" android:paddingRight="4dip"
android:orientation="horizontal">
<TextView android:id="@+id/tv_tags" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_weight="1"
android:textColor="#000" android:onClick="actv_item_click" />
<CheckBox android:id="@+id/cb_tags" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:onClick="actv_item_checked" />
</LinearLayout>
因此,结果将按我所希望的方式显示。但是TextView的onClick侦听器没有响应。我也不知道如何在(取消)选择某个项目后访问数据
列表的行为应如下所示:
- 点击复选框项目应将相应的标记插入/附加到AutoCompleteTextView中(标记将以分号分隔)
- 点击TextView项应将相应的标记插入/添加到AutoCompleteTextView并关闭列表