使用 AutoCompleteTextView 自定义光标到文本
我通过扩展 AutoCompleteTextView (使用名为 setCursorAdapter 的专用函数)创建了一个自定义视图。我提供 SimpleCursorAdapter 并添加 FilterQueryProvider。我将其用于数字列表,我需要能够在数字中的任何位置搜索匹配项。
public void setCursorAdapter(final Uri uri, final String key) {
Cursor c = getContext().getContentResolver().query(uri,
new String[] { "_id", key }, null, null, key);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(),
android.R.layout.simple_dropdown_item_1line, c,
new String[] { key }, new int[] { android.R.id.text1 });
this.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((SimpleCursorAdapter) getAdapter()).getFilterQueryProvider()
.runQuery(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Cursor c = getContext().getContentResolver().query(uri,
new String[] { "_id", key },
key + " LIKE '%" + constraint + "%'", null, key);
return c;
}
});
setAdapter(adapter);
}
一切都工作正常,直到我从下拉列表中选择值。我在 EditText 窗口中收到的值: android.content.ContentResolver$CursorWrapperInner@...
如何避免这种情况并让我的(数字)文本正确显示。
谢谢
I have created a custom view by extending AutoCompleteTextView (with a specialized function called setCursorAdapter). I am supplying a SimpleCursorAdapter and adding a FilterQueryProvider. I am using this for a list of numbers where I need the ability to search anywhere within the number for a match.
public void setCursorAdapter(final Uri uri, final String key) {
Cursor c = getContext().getContentResolver().query(uri,
new String[] { "_id", key }, null, null, key);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(getContext(),
android.R.layout.simple_dropdown_item_1line, c,
new String[] { key }, new int[] { android.R.id.text1 });
this.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
((SimpleCursorAdapter) getAdapter()).getFilterQueryProvider()
.runQuery(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
adapter.setFilterQueryProvider(new FilterQueryProvider() {
@Override
public Cursor runQuery(CharSequence constraint) {
Cursor c = getContext().getContentResolver().query(uri,
new String[] { "_id", key },
key + " LIKE '%" + constraint + "%'", null, key);
return c;
}
});
setAdapter(adapter);
}
Everything is working perfectly, until I select the value from the drop down. The value that I receive in the EditText window: android.content.ContentResolver$CursorWrapperInner@...
How do I avoid this and get my (numeric) text displayed correctly.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已解决:SimpleCursorAdapter.converToString(cursor) - 我重写了此函数以提取值。
谢谢
Solved: SimpleCursorAdapter.converToString(cursor) -- I overrode this function to extract the value.
Thanks
更好的方法是使用 public void setStringConversionColumn (int stringConversionColumn)
The better way is to use public void setStringConversionColumn (int stringConversionColumn)