我正在显示已过滤的Listview,如下所示。
public class SimpleArrayAdapter extends BaseAdapter implements Filterable {
private Context context;
private ServicesFilter filter;
public SimpleArrayAdapter(Context _context, ArrayList<Services> values) {
context = _context;
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new ServicesFilter();
}
return filter;
}
@Override
public int getCount() {
return filteredServices.size();
}
@Override
public Object getItem(int position) {
return filteredServices.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.job_review_cell, null);
}
TextView txt_Desc = (TextView) convertView.findViewById(R.id.txt_Content);
Services cat = (Services)getItem(position);
txt_Desc.setText(cat.getCategoryName());
return convertView;
}
private class ServicesFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
String searchTerm = constraint.toString().toLowerCase();
ArrayList<Services> filteredItems = new ArrayList<>();
for(int i=0;i<services.size();i++)
{
Services cat = services.get(i);
String catName = cat.getCategoryName().toLowerCase();
if(catName.contains(searchTerm))
{
filteredItems.add(cat);
}
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized (this)
{
result.values = services;
result.count = services.size();
}
}
return result;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredServices = (ArrayList<Services>)results.values;
//Hide or show the results list view
if(filteredServices.size() == services.size())
listContainer.setVisibility(View.GONE);
else if(filteredServices.size() > 0)
listContainer.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}
}
这很好用。但是现在我希望listview最初加载项目并从中过滤。
这是TextWatcher
private TextWatcher textWatcher = 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) {
searchAdapter.getFilter().filter(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
};
怎么做?
答案 0 :(得分:0)
您必须在工具中使用SearchView。
<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();
}
return super.onCreateOptionsMenu(menu);
}