【发布时间】:2017-05-09 20:04:55
【问题描述】:
我正在处理AutoCompleteTextView。适配器没有根据我的OnTextChangeListener 填充任何数据,它在初始化时是第一次。
public void fn_LoadLrnoDetail(ArrayList<String> lst_Name) {
try {
progressBar.showDialog();
Adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, new ArrayList<String>());
autotxtLrno.setAdapter(Adapter);
if (lst_Name != null && lst_Name.size() > 0) {
Adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, lst_Name);
autotxtLrno.setAdapter(Adapter);
Adapter.notifyDataSetChanged();
}
progressBar.hideDialog();
} catch (Exception e) {
Log.e(TAG, "fn_LoadLrnoDetail: " + e.getLocalizedMessage());
}
}
这是我的AutoCompleteTextView 方法。问题是无论第一次有什么数据,即使数据根据OnTextChangeListener发生变化,它仍然保持不变。
autotxtLrno.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
halfClear();
custom_list.clear();
Log.e(TAG, "onTextChanged: " + lst_Name.size() + " " + lst_Name);
for (String custom : lst_Name) {
if (custom.contains(autotxtLrno.getText().toString())) {
custom_list.add(custom);
}
}
Log.e(TAG, "custom: " + custom_list + " " + custom_list.size());
if (custom_list.size() > 0) {
fn_LoadLrnoDetail(custom_list);
}
}
@Override
public void afterTextChanged(Editable s) {
Adapter.notifyDataSetChanged();
}
});
这是OnTextChangedListener 的方法。如果我更改了文本,则不会填充任何内容。
你能告诉我我在哪里犯了错误。
任何帮助将不胜感激。提前致谢
【问题讨论】:
-
你的数据来自 sqlite 数据库,对吧?所以使用
SimpleCursorAdapter并设置它的FilterQueryProvider:可能需要10 行代码,仅此而已,不需要任何TextWatcher -
是的。我的数据来自
SQLite,知道如何实现吗? -
我已经告诉过你:使用
SimpleCursorAdapter并设置它的FilterQueryProvider,你需要做的就是从runQuery返回一个Cursor方法:最多10 行代码 -
如果您希望它更短,那么 10 行代码会覆盖您自定义
runQueryOnBackgroundThread()中的runQueryOnBackgroundThread() -
仍然无法做到这一点
标签: android autocomplete android-arrayadapter