[英]EditText search item in listview doesn't work
Hi guys i'm implementing a edittext in my listview to filter the items. 嗨,大家好,我正在实现listview的edittext来过滤项目。 I'm following this example, but nothing happen.
我正在跟踪此示例,但是什么也没有发生。 It doesn't work and when i try to filter it doesn't do what i want.. This is my part of code of edittext:
它不起作用,当我尝试过滤时,它并没有实现我想要的功能。这是我的edittext代码的一部分:
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setTextFilterEnabled(true);
lv.setAdapter(listadaptor);
inputSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.listadaptor.getFilter().filter(cs);
}
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
};
My listview is a list that show all application installed in the device.. I have this to load the list: 我的列表视图是一个列表,显示了设备中安装的所有应用程序。
private class LoadApplications extends AsyncTask<Void, Void, Void> {
public ProgressDialog progress = null;
@Override
protected Void doInBackground(Void... params) {
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
listadaptor = new ApplicationAdapter(MainActivity.this,R.layout.snippet_list_row, applist);
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
protected void onDestroy() {
if(progress!=null)
if(progress.isShowing()){
progress.dismiss();
}
}
@Override
protected void onPostExecute(Void result) {
setListAdapter(listadaptor);
progress.dismiss();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(MainActivity.this, null,
"Caricamento applicazioni in corso...");
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
} }
so listadaptor
is my adapter.. Someone can help me? 所以
listadaptor
是我的适配器。有人可以帮助我吗? Thanks 谢谢
Try this one .. 试试这个..
ArrayList<String> type_name_copy = new ArrayList<String>();
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable theWatchedText) {
ArrayList<String> type_name_filter = new ArrayList<String>();
String text = theWatchedText.toString();
for (int i = 0; i < array.size(); i++) {
if ((array.get(i).toLowerCase()).contains(text
.toLowerCase())) {
type_name_filter.add(array.get(i));
}
}
type_name_copy = type_name_filter;
listUpdate(type_name_copy);
}
});
}
public void listUpdate(ArrayList<String> data) {
lv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, data));
}
}
我建议您使用AutocompleteTextView,它是具有内置android搜索功能的Edittext。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.