Android自定义listView适配器过滤器不会返回到原始列表

时间:2013-07-31 05:58:31

标签: android baseadapter

我编辑了我的Adapter类并扩展了可过滤的类。我的搜索功能有效但如果我退回搜索菜单中的所有内容,我将无法返回原始内容。

这是我的适配器类:

public class Menu_List_Item_Adapter extends BaseAdapter implements Filterable {
Context context;
List<FoodMenuRowItem> rowItems;

public Menu_List_Item_Adapter(Context context, List<FoodMenuRowItem> items) {
    this.context = context;
    this.rowItems = items;
}   
private class ViewHolder {
    ImageView imageView;
    TextView txtTitle;
    TextView txtDesc;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.food_menu_item_list_row,
                null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.price);
        holder.txtTitle = (TextView) convertView
                .findViewById(R.id.description);
        holder.imageView = (ImageView) convertView.findViewById(R.id.img);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    FoodMenuRowItem rowItem = (FoodMenuRowItem) getItem(position);

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());
    holder.imageView.setImageResource(rowItem.getImageId());

    return convertView;
}

@Override
public int getCount() {
    return rowItems.size();
}

@Override
public Object getItem(int position) {
    return rowItems.get(position);
}

@Override
public long getItemId(int position) {
    return rowItems.indexOf(getItem(position));
}

@Override
public Filter getFilter() {
    return new Filter() {
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {

            if (results.count == 0)
                notifyDataSetInvalidated();
            else {
                rowItems = (List<FoodMenuRowItem>) results.values;
                Menu_List_Item_Adapter.this.notifyDataSetChanged();
            }

        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults results = new FilterResults();

            if (constraint == null || constraint.length() == 0) {
                // No filter implemented we return all the list
                results.values = rowItems;
                results.count = rowItems.size();

            } else { // We perform filtering operation
                List<FoodMenuRowItem> filteredRowItems = new ArrayList<FoodMenuRowItem>();

                for (FoodMenuRowItem p : rowItems) {
                    if (p.getName()
                            .toUpperCase()
                            .startsWith(constraint.toString().toUpperCase())) {
                        filteredRowItems.add(p);
                    }
                }
                results.values = filteredRowItems;
                results.count = filteredRowItems.size();
            }
            return results;
        }
    };
}

}

我会检查结果是零还是空,如果是,返回原始列表? 我错过了什么?

3 个答案:

答案 0 :(得分:3)

您应该存储rowItems以返回它,然后在搜索区域获取空文本后,您应该将该数组恢复到rowItems,以便适配器可以重新加载自身。类似的东西:

public class Menu_List_Item_Adapter extends BaseAdapter implements Filterable {
Context context;
List<FoodMenuRowItem> rowItems;
List<FoodMenuRowItem> storedRowItems;

public Menu_List_Item_Adapter(Context context, List<FoodMenuRowItem> items) {
this.context = context;
this.rowItems = items;
this.storedRowItems = new List<FoodMenuRowItem>(items);
}   

// ...

@Override
public Filter getFilter() {
return new Filter() {
    @SuppressWarnings("unchecked")
    @Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {

        if (results.count == 0)
            notifyDataSetInvalidated();
        else {
            rowItems = (List<FoodMenuRowItem>) results.values;
            Menu_List_Item_Adapter.this.notifyDataSetChanged();
        }

    }

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {

        FilterResults results = new FilterResults();

        if (constraint == null || constraint.length() == 0) {
            // No filter implemented we return all the list
            results.values = storedRowItems;
            results.count = storedRowItems.size();

        } else { // We perform filtering operation
            List<FoodMenuRowItem> filteredRowItems = new ArrayList<FoodMenuRowItem>();

            rowItems = storedRowItems;
            for (FoodMenuRowItem p : rowItems) {
                if (p.getName()
                        .toUpperCase()
                        .startsWith(constraint.toString().toUpperCase())) {
                    filteredRowItems.add(p);
                }
            }
            results.values = filteredRowItems;
            results.count = filteredRowItems.size();
        }
        return results;
    }
};
}

答案 1 :(得分:1)

它不会返回original list,因为当您将对象列表传递给custom adapter时,adapter会引用列表的地址,当您搜索某些内容时,它会更改列表在那个地址。但是当您按退格键适配器时不会恢复列表。所以重点是我们创建了另一个List<Object> nlist = new ArrayList<Object>(originalList);,这意味着列表是在具有相同内容的另一个位置生成的。现在使用nlist恢复列表。

答案 2 :(得分:0)

请按照更改进行操作。

  1. 创建临时变量以存储初始数据

    List<FoodMenuRowItem> tempitems=new ArrayList<FoodMenuRowItem>();
    
  2. 在构造函数中初始化temperory变量

        this.tempitems=items;
    
  3. 将performFiltering方法中的for循环替换为

            for (int i = 0; i < tempitems.size(); i++) {
            filterableString = tempitems.get(i);
            if (filterableString.getUserName().toLowerCase()
                    .contains(filterString)) {
                Filtered_Names.add(filterableString);
    
                Log.v("riyas:" + filterableString.getUserName(),
                        "riyas filtering");
            }
        }