带有自定义列表的 AutoCompleteTextView:如何设置 OnItemClickListener
我正在开发一个使用标签的应用程序。访问这些内容应该尽可能简单。使用 AutoCompleteTextView 似乎适合我。我想要的:
- 现有的标签应该显示在一个可选列表中,每个项目的侧面都有一个复选框
- 现有的标签应该在 AutoCompleteTextView 的焦点上显示(即不是在输入字母后)
到目前为止我所做的是将标签存储在专用的sqlite3 表。查询标签会产生游标。光标被传递到 SimpleCursorAdapter ,如下所示:
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);
如您所见,我创建了 tags_row.xml ,如下所示:
<?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>
它看起来像这样:
image http://img708.imageshack.us/img708/5992/devicem.png
所以结果显示就像我一样希望他们这样做。但 TextView 的 onClick 侦听器没有响应。而且我不知道如何在(取消)选择某个项目后访问数据。
列表的行为应如下:
- 点击 CheckBox 项目应将相应的标签插入/附加到 AutoCompleteTextView 中(标签将用分号分隔)
- 点击 TextView 项目应将相应的标签插入/附加到 AutoCompleteTextView 中并关闭列表。
I am working on an app which uses tags. Accessing those should be as simple as possible. Working with an AutoCompleteTextView seems appropriate to me. What I want:
- existing tags should be displayed in a selectable list with a CheckBox on each item's side
- existing tags should be displayed UPON FOCUS of AutoCompleteTextView (i.e. not after typing a letter)
What I've done so far is storing tags in a dedicated sqlite3 table. Tags are queried resulting in a Cursor. The Cursor is passed to a SimpleCursorAdapter which looks like this:
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);
As you can see I created tags_row.xml which looks like this:
<?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>
It looks like this:
image http://img708.imageshack.us/img708/5992/devicem.png
So the results are displayed just as I'd want them to. But the TextView's onClick listener does not respond. And I don't have a clue on how to access the data once an item is (de-)selected.
Behaviour of the list should be the following:
- tapping a CheckBox item should insert/append the corresponding tag into the AutoCompleteTextView (tags will be semicolon-seperated)
- tapping a TextView item should insert/apped the corresponding tag into the AutoCompleteTextView AND close the list.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论