如何在android的自定义listview中实现搜索?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21827646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to implement search in custom listview in android?
提问by Ali
I have an edittext and a listview in my application my listview show contact list. I want listview filter with edittext. I searched a lot on google and found some examles but none worked for me here's my code
my custom adapter
我的应用程序中有一个编辑文本和一个列表视图,我的列表视图显示联系人列表。我想要带有编辑文本的列表视图过滤器。我在谷歌上搜索了很多,找到了一些例子,但没有一个对我有用,这是我的代码,
我的自定义适配器
public class ContactListAdapter extends ArrayAdapter {
private final Activity activity;
private final List<ContactStock> stocks;
private ArrayList<ContactStock> arraylist;
public ContactListAdapter(Activity activity, List<ContactStock> objects) {
super(activity, R.layout.listview_detail, objects);
this.activity = activity;
this.stocks = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView = convertView;
ContactStockView sv = null;
if (rowView == null) {
// Get a new instance of the row layout view
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(
R.layout.listview_detail, null);
// Hold the view objects in an object,
// so they don't need to be re-fetched
sv = new ContactStockView();
sv.name = (TextView) rowView.findViewById(R.id.textView1);
sv.number = (TextView) rowView.findViewById(R.id.textView2);
// Cache the view objects in the tag,
// so they can be re-accessed later
rowView.setTag(sv);
} else {
sv = (ContactStockView) rowView.getTag();
}
// Transfer the stock data from the data object
// to the view objects
ContactStock currentStock = (ContactStock) stocks.get(position);
sv.name.setText(currentStock.getName());
sv.number.setText(currentStock.getNumber());
// TODO Auto-generated method stub
return rowView;
}
protected static class ContactStockView {
protected TextView name;
protected TextView number;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
stocks.clear();
if (charText.length() == 0) {
stocks.addAll(arraylist);
} else {
for (ContactStock cs : arraylist) {
if (cs.getName().contains(charText)) {
stocks.add(cs);
}
}
}
notifyDataSetChanged();
}
}
Main class edittext code is
主类edittext代码是
edittext = (EditText)findViewById(R.id.editText1);
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
//MainActivity.this.adapt.getFilter().filter(s);
String searchString=edittext.getText().toString();
adapt.filter(searchString);
}
@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
}
});
without custom adapter it is working with getfilter(). But I don't know how to filter with custom adapter. any help will be appriciated. thanks in advance
没有自定义适配器,它可以与 getfilter() 一起使用。但我不知道如何使用自定义适配器进行过滤。任何帮助都会得到帮助。提前致谢
回答by Ali
This one worked for me:
这个对我有用:
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
int textlength = cs.length();
ArrayList<ContactStock> tempArrayList = new ArrayList<ContactStock>();
for(ContactStock c: arraylist){
if (textlength <= c.getName().length()) {
if (c.getName().toLowerCase().contains(cs.toString().toLowerCase())) {
tempArrayList.add(c);
}
}
}
mAdapter = new ContactListAdapter(activity, tempArrayList);
lv.setAdapter(mAdapter);
}
回答by Rachita Nanda
Android has an built in search bar feature.
Android 有一个内置的搜索栏功能。
- You can use this search bar to get input from the user .Can refer to this linklink2
- Create an async taskwhich will query your database/data to fetch matching results and populate the listview again
Step 1Add the following in your manifest :
步骤 1在清单中添加以下内容:
<application
android:allowBackup="true"
android:icon="@drawable/dolphin1"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
<meta-data
android:name="android.app.default_searchable"
android:value="com.RareMediaCompany.MuditaBulkScanner.DocketListActivity" />
</application>
Step2
第2步
onSearchRequested(); will call your search bar .You can add this in on click of your search button.
onSearchRequested(); 将调用您的搜索栏。您可以通过单击搜索按钮将其添加。
Step3Simply call this function in your activity's on create :
Step3只需在您的活动的 on create 中调用此函数:
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
myList.clear();
String query = intent.getStringExtra(SearchManager.QUERY);
new getQueryResultAndPopulateListTask(this, query).execute();
}
}