Android Action Bar Search:如何通过列表项的属性过滤列表? [英] Android Action Bar Search: How to filter a list by the property of a list item?
问题描述
我有一个带有列表项的列表视图,其中有几个文本视图和一个复选框.使用操作栏搜索,我需要通过文本视图值过滤掉列表.
I have a list view with a list item, which has few text views and a checkbox. Using action bar search, I need to filter out the list by a text view value.
这是我需要按优先级"过滤的列表.
This is my list which I need to filter by "Priority".
这是我用来过滤数据采用者列表的方法.
This is the method I used to filter the list from the data adopter.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
SearchView.OnQueryTextListener textChangeListener = new SearchView.OnQueryTextListener()
{
@Override
public boolean onQueryTextChange(String newText)
{
// this is your adapter that will be filtered
dataAdapter.getFilter().filter(newText);
System.out.println("on text chnge text: "+newText);
return true;
}
@Override
public boolean onQueryTextSubmit(String query)
{
// this is your adapter that will be filtered
dataAdapter.getFilter().filter(query);
System.out.println("on query submit: "+query);
return true;
}
};
searchView.setOnQueryTextListener(textChangeListener);
return super.onCreateOptionsMenu(menu);
}
我已将 dataAdopter 设置为包含很少元素的列表视图,即 3 个文本视图和一个复选框.
I have set the dataAdopter to my listview which contains few elements, i.e 3 text views and a check box.
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.task_info, taskList);
listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
但是当我按照以下方式设置适配器时;只使用一个字符串数组值,过滤没有任何问题
But when I set the adapter in the following way; using just a string array of values, the filtering happens without any issues
String[] dataArray = new String[] {"High","Medium", "Low", "Medium", "High", "Low","High"};
listView = (ListView) findViewById(R.id.listview);
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataArray);
listView.setAdapter(myAdapter);
有什么方法可以用来通过列表项内的文本视图的值来过滤列表?或者是否有任何替代方法来实现所需的功能.
Is there any method that I can use to filter the list by the value of a text view inside a list item? Or is there any alternative method to implement the desired functionality.
推荐答案
最有效的方法是在您的适配器上实现过滤器.为此,您必须实现接口 Filterable(实现 Filterable).然后像这样实现方法getFilter.
the most efficient way is to implement a filter on your adapter. For this you must implement the interface Filterable (implements Filterable). Then implement the method getFilter like this.
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
List<Object> filteredResult = getFilteredResults(charSequence);
FilterResults results = new FilterResults();
results.values = filteredResult;
results.count = filteredResult.size();
return results;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
listaFiltrada = (ArrayList<Documento>) filterResults.values;
MyAdapter.this.notifyDataSetChanged();
}
private ArrayList<Object> getFilteredResults(CharSequence constraint){
if (constraint.length() == 0){
return listaDocumentos;
}
ArrayList<Object> listResult = new ArrayList<Object>();
for (Object obj : listaTotal){
if (condition){
listResult.add(obj);
}
}
return listResult;
}
};
}
最后,在您的 Fragment 或 Activity 中,在方法 onCreateOptionsMenu 中获取 EditText(搜索)并添加:
Finally, in your Fragment or Activity, in the method onCreateOptionsMenu get the EditText (search) and add this:
final EditText editText = (EditText) menu.findItem(R.id.menu_buscar).getActionView();
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
if (myAdapter != null) {
myAdapter.getFilter().filter(charSequence);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
希望对你有帮助:)
这篇关于Android Action Bar Search:如何通过列表项的属性过滤列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!