0

我已经编辑了我的 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;
        }
    };
}

}

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

4

3 回答 3

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;
    }
};
}
于 2013-07-31T06:04:26.877 回答
1

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

于 2013-09-18T12:07:40.583 回答
0

请关注更改。

  1. 创建一个临时变量来存储初始数据

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

        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");
            }
        }
    
于 2013-12-12T12:58:05.147 回答