你好,在我的项目中,我创建了一个名为&#34的对象类;程序"有许多值,例如一个名为"类别的数组。"所有程序都由ListView
显示,并在布局中设置了类别Spinner
(目的是使用匹配的类别对程序进行分类)。
我有这段代码:
final ArrayAdapter spinnerAdapter = new ArrayAdapter(getActivity(), R.layout.layout_spinner, MainActivity.categories); //MainActivity.categories is an array filled with strings
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = spinner.getSelectedItem().toString();
listAdapter.getFilter().filter(text);
listAdapter.notifyDataSetChanged();
}
Program.java
public class Program {
public String programName;
public ArrayList<String> categories;
.....
public ArrayList<String> getCategories () {
return categories;
}
public String toString() {
return programName;
}
}
但这仅根据列表中的文字进行过滤,如何通过程序列表中的Category
变量对其进行过滤?
答案 0 :(得分:0)
那么,
首先来看看这里并了解一下过滤:
接下来,我认为在您的情况下,您必须执行以下操作:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
// We implement here the filter logic
if (constraint == null || constraint.length() == 0) {
// No filter implemented we return all the list
results.values = yourList;
results.count = yourList.size();
}
else {
// We perform filtering operation
List<Program> finalList = new ArrayList<Program>();
//here think of something regarding the upper and lower cases.
for (Program p : yourList) {
if(p.getCategories().
contains(constraint.toString()))
finalList.add(p);
}
results.values = finalList;
results.count = finalList.size();
}
return results;
}