我实现了listview,现在我想在list-view
上实现搜索功能这是我的代码
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return true;
}
如何在list-view上实现搜索。我在搜索和更新列表后也没有获得如何实现其他功能
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Inflate menu to add items to action bar if it is present.
inflater.inflate(R.menu.menu_product_detail, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.getQuery();
Log.v("Abhijit", "" + searchManager.QUERY.toString());
searchView.setOnQueryTextListener(this);
return true;
}
如何在list-view上实现搜索。我在搜索和更新列表后也没有获得如何实现其他功能
答案 0 :(得分:0)
使用此代码:
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
活动文件:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.dashboard, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) MainActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = null;
if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();
}
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(MainActivity.this.getComponentName()));
}
return super.onCreateOptionsMenu(menu);
}
清单文件:
<meta-data
android:name="android.app.default_searchable"
android:value="com.apkgetter.SearchResultsActivity" />
<activity
android:name="com.apkgetter.SearchResultsActivity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<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>
可搜索的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/search_hint"
android:label="@string/app_name" />
为您实施可过滤的listview适配器,以根据搜索到的数据过滤列表。
答案 1 :(得分:0)
这是你在适配器中过滤的方式:
public class FavouritesAdapter extends RecyclerView.Adapter<FavouritesAdapter.MyViewHolder> implements Filterable {
private ItemFilter mFilter = new ItemFilter();
private ArrayList<ProductDetail> listItems, filterList;
private Context context;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public MyViewHolder(View view) {
super(view);
textView = (TextView) view.findViewById(R.id.textView);
}
}
public FavouritesAdapter(Context context, List<ProductDetail> listItems) {
this.context = context;
this.listItems = listItems;
this.filterList = new ArrayList<ProductDetail>();
// we copy the original list to the filter list and use it for setting row values
this.filterList.addAll(this.listItems);
}
@Override
public FavouritesAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_favourites, parent, false);
return new FavouritesAdapter.MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(FavouritesAdapter.MyViewHolder holder, int position) {
holder.textView.setText(filterList.get(position));
}
// Clean all elements of the recycler
public void clear() {
filterList.clear();
notifyDataSetChanged();
}
// Add a list of items
public void addAll(List<ProductDetail> list) {
filterList.addAll(list);
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return (null != filterList ? filterList.size() : 0);
}
@Override
public Filter getFilter() {
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
final Calendar calendar = Calendar.getInstance();
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
final List<ProductDetail> list = listItems;
int count = list.size();
final ArrayList<ProductDetail> nlist = new ArrayList<ProductDetail>(count);
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = list.get(i).getYourObject; // get Your Object against whom you want to compare the list
if (filterableString.toLowerCase().contains(filterString)) {
nlist.add(listItems.get(i));
}
}
results.values = nlist;
results.count = nlist.size();
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
filterList = (ArrayList<ProductDetail>) results.values;
notifyDataSetChanged();
}
}
}
在您的活动中:
adapter.getFilter().filter(searchedEditText.getText().toString());