[英]AutoCompleteTextView Adapter not dropdown onTextChange?
我正在使用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
的方法。 如果我更改了文本,則不會填充任何內容。 你能告訴我我在哪里犯錯。
任何幫助將不勝感激。 提前致謝
問題是無論什么時候數據都是第一次,即使根據OnTextChangeListener更改了數據,它也保持不變
解決方案 :
每次數據更改時都在創建新適配器。
if (lst_Name != null && lst_Name.size() > 0) {
This line--> Adapter = new ArrayAdapter<String>(getContext(),
android.R.layout.simple_dropdown_item_1line, lst_Name);
autotxtLrno.setAdapter(Adapter);
Adapter.notifyDataSetChanged();
而是將您的adpater初始化移動到clear的oncreate方法,並將適配器設置為listView, notifyDataSetChanged
像這樣調用notifyDataSetChanged
方法
Adapter.clear();
Adapter.add(lst_name)
autotxtLrno.setAdapter(Adapter);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.