我已经在列表中实现了自定义过滤器但没有工作,有些机构可以告诉我代码中的问题在哪里吗?
这是我的创建方法。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testsample);
sv = (SearchView) findViewById(R.id.searchView);
item_list = (ArrayList<Item_Category>) getLastNonConfigurationInstance();
if (item_list == null) {
lv = (ListView) findViewById(R.id.listView3);
lv.setItemsCanFocus(false);
lv.setChoiceMode(lv.CHOICE_MODE_MULTIPLE);
item_list = new ArrayList<Item_Category>();
item_list.add(new Item_Category("gold", false));
item_list.add(new Item_Category("sugar", false));
item_list.add(new Item_Category("gulli", false));
item_list.add(new Item_Category("silver", false));
item_list.add(new Item_Category("chmacham", false));
arrayList = new ArrayList<Item_Category>();
arrayList.addAll(item_list);
arrayAdapter = new ItemCategoryArrayAdapter(this, arrayList);
lv.setAdapter(arrayAdapter);
}
sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String text) {
selected = text.trim();
//lv = (ListView)findViewById(R.id.listView);
if (arrayAdapter != null) {
arrayAdapter.getFilter().filter(selected);
lv.setAdapter(arrayAdapter);
return true;
}
return true;
}
});
}
全部用于自定义列表视图和自定义过滤器..
private class Item_Category
{
String category_Name ;
boolean checked ;
public String getCategory_Name() {
return category_Name;
}
public void setCategory_Name(String category_Name) {
this.category_Name = category_Name;
}
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public Item_Category(String category_Name, boolean checked) {
this.category_Name = category_Name;
this.checked = checked;
}
}
private static class Item_Category_ViewHolder
{
TextView categoryName;
CheckBox checkBox;
public TextView getCategoryName() {
return categoryName;
}
public void setCategoryName(TextView categoryName) {
this.categoryName = categoryName;
}
public CheckBox getCheckBox() {
return checkBox;
}
public void setCheckBox(CheckBox checkBox) {
this.checkBox = checkBox;
}
public Item_Category_ViewHolder(TextView categoryName, CheckBox checkBox) {
this.categoryName = categoryName;
this.checkBox = checkBox;
}
}
public class ItemCategoryArrayAdapter extends ArrayAdapter<Item_Category> implements Filterable {
private LayoutInflater inflater;
ArrayList<String> itemList;
List<Item_Category> filtered;
List<Item_Category> mStringFilterList;
ItemCategoryFilter categoryFilter;
public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) {
super(context, R.layout.everyday_item_category_row, R.id.itemName, categoryList);
// Cache the LayoutInflate to avoid asking for a new one each time.
filtered = new ArrayList<Item_Category>(categoryList);
this.inflater = LayoutInflater.from(context);
mStringFilterList = new ArrayList<Item_Category>(categoryList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Planet to display
Item_Category item_list = (Item_Category) this.getItem(position);
// The child views in each row.
CheckBox checkBox;
TextView itemCategory;
// Create a new row view
if (convertView == null) {
convertView = inflater.inflate(R.layout.everyday_item_category_row, null);
// Find the child views.
itemCategory = (TextView) convertView.findViewById(R.id.itemName);
checkBox = (CheckBox) convertView.findViewById(R.id.CheckBox);
// Optimization: Tag the row with it's child views, so we don't have to
// call findViewById() later when we reuse the row.
convertView.setTag(new Item_Category_ViewHolder(itemCategory, checkBox));
// If CheckBox is toggled, update the planet it is tagged with.
checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Item_Category item_list1 = (Item_Category) cb.getTag();
item_list1.setChecked(cb.isChecked());
}
});
}
// Reuse existing row view
else {
// Because we use a ViewHolder, we avoid having to call findViewById().
Item_Category_ViewHolder viewHolder = (Item_Category_ViewHolder) convertView.getTag();
checkBox = viewHolder.getCheckBox();
itemCategory = viewHolder.getCategoryName();
}
// Tag the CheckBox with the Planet it is displaying, so that we can
// access the planet in onClick() when the CheckBox is toggled.
checkBox.setTag(item_list);
// Display planet data
checkBox.setChecked(item_list.isChecked());
itemCategory.setText(item_list.getCategory_Name());
CheckBox c1 = (CheckBox) convertView.findViewById(R.id.CheckBox);
final TextView t1 = (TextView) convertView.findViewById(R.id.itemName);
c1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
itemList.add(t1.getText().toString());
} else {
if (itemList.contains(t1.getText().toString())) {
itemList.remove(t1.getText().toString());
}
}
}
});
return convertView;
}
@Override
public Filter getFilter() {
if (categoryFilter == null)
categoryFilter = new ItemCategoryFilter();
return categoryFilter;
}
private class ItemCategoryFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
mStringFilterList = new ArrayList<Item_Category>();
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<Item_Category> filterList = new ArrayList<Item_Category>();
for (int i = 0; i < mStringFilterList.size(); i++) {
if ((mStringFilterList.get(i).getCategory_Name().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
Item_Category itemCategory = new Item_Category(mStringFilterList.get(i)
.getCategory_Name(), false);
filterList.add(itemCategory);
}
}
results.count = filterList.size();
results.values = filterList;
} else {
results.count = mStringFilterList.size();
results.values = mStringFilterList;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
// Now we have to inform the adapter about the new list filtered
filtered = (ArrayList<Item_Category>) results.values;
notifyDataSetChanged();
}
}
答案 0 :(得分:1)
为自定义网格视图或列表视图创建适配器,在过滤时都存在一些问题。您可以看到,适配器不会过滤网格或列表视图中的数据。
我无法粘贴代码,但我建议您查看ArrayAdapter.class
的实现。确保ItemCategoryArrayAdapter.class
中包含大多数方法,ArrayAdapter.class
。
确保ArrayAdapter.class
中的大多数方法都在ItemCategoryArrayAdapter.class
中实现。
这将解决它......
答案 1 :(得分:0)
按如下方式更改适配器构造函数。
public ItemCategoryArrayAdapter(Context context, List<Item_Category> categoryList) {
filtered = new ArrayList<Item_Category>(categoryList);
super(context, R.layout.everyday_item_category_row, R.id.itemName, filtered);
// Cache the LayoutInflate to avoid asking for a new one each time.
您正在传递完整列表,即categoryList而不是已过滤的List,因此它没有反映。