ListView中的自定义筛选ArrayAdapter不适用于筛选来自localhost服务器的映像

时间:2013-01-04 06:38:52

标签: android filter android-listview filtering android-arrayadapter

我正在过滤listview,其中我正在过滤来自localhost的数据和图像...过滤哪些数据但不过滤图像。当我搜索到的数据来了,但图像来自其原始位置意味着图像来自其原始位置......

public class Listadapter extends ArrayAdapter<HashMap<String, String>> {

    ArrayList<HashMap<String, String>> originalList;
    ArrayList<HashMap<String, String>> prodlist;
    private ProductFilter filter;

    public Listadapter(Context context, int textViewResourceId,
            ArrayList<HashMap<String, String>> Strings) {
        super(context, textViewResourceId, Strings);
        this.prodlist = new ArrayList<HashMap<String, String>>();
        this.prodlist.addAll(productList);
        this.originalList = new ArrayList<HashMap<String, String>>();
        this.originalList.addAll(productList);
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new ProductFilter();
        }
        return filter;
    }

    private class ViewHolder {
        TextView txtprodName;
        TextView txtcategory;
        TextView txtOfferDate;
        ImageView ProductImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));
        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.product_list_item, null);

            holder = new ViewHolder();
            holder.txtprodName = (TextView) convertView
                    .findViewById(R.id.txtprodName);
            holder.txtcategory = (TextView) convertView
                    .findViewById(R.id.txtcategory);
            holder.txtOfferDate = (TextView) convertView
                    .findViewById(R.id.txtOfferDate);
            holder.ProductImage = (ImageView) convertView
                    .findViewById(R.id.ProductImage);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> hm = prodlist.get(position);

        holder.txtprodName.setText(hm.get(TAG_PRODUCT_NAME));
        holder.txtcategory.setText(hm.get(TAG_CATEGORY_NAME));
        holder.txtOfferDate.setText(hm.get(TAG_OFFER_START_TIME));

        if (drawable.get(position) != null)
            holder.ProductImage.setImageDrawable(drawable.get(position));
        else
            holder.ProductImage.setImageResource(R.drawable.nopic_place);

        return convertView;
    }

    private class ProductFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String, String>>();
                for (int i = 0, l = originalList.size(); i < l; i++) {
                    HashMap<String, String> p = originalList.get(i);
                    if (p.toString().toLowerCase().contains(constraint))
                        filteredItems.add(p);
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            } else {
                synchronized (this) {
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            // TODO Auto-generated method stub
            prodlist = (ArrayList<HashMap<String, String>>) results.values;

            notifyDataSetChanged();
            clear();
            for (int i = 0, l = prodlist.size(); i < l; i++)
                add(prodlist.get(i));
            notifyDataSetInvalidated();

        }

    }

}

2 个答案:

答案 0 :(得分:0)

您应该按以下方式过滤列表 drawable

创建另一个列表,将其命名为 prodDrawable ,例如:

ArrayList <Drawable> prodDrawable;

ArrayAdapter构造函数中,按以下方式初始化列表:

this.prodDrawable = new ArrayList <Drawable> ();
this.prodDrawable.addAll ( drawable );

使用 getView 方法中的新列表(而不是 drawable 列表):

try {
    holder.ProductImage.setImageDrawable(prodDrawable.get(position));
} catch ( Exception exception ) {
    holder.ProductImage.setImageResource(R.drawable.nopic_place);
}

最后,在过滤器中,您也应过滤图片列表,更改此内容:

if (constraint != null && constraint.toString().length() > 0) {
    ArrayList<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String, String>>();
    for (int i = 0, l = originalList.size(); i < l; i++) {
        HashMap<String, String> p = originalList.get(i);
        if (p.toString().toLowerCase().contains(constraint))
            filteredItems.add(p);
    }
    result.count = filteredItems.size();
    result.values = filteredItems;
} else {
    synchronized (this) {
        result.values = originalList;
        result.count = originalList.size();
    }
}

THIS

if (constraint != null && constraint.toString().length() > 0) {
    ArrayList<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String, String>>();
    prodDrawable.clear ();
    for (int i = 0, l = originalList.size(); i < l; i++) {
        HashMap<String, String> p = originalList.get(i);
        if (p.toString().toLowerCase().contains(constraint)) {
            filteredItems.add(p);
            try {
                prodDrawable.add ( drawable.get ( i ) );
            } catch ( Exception exception ) {
                prodDrawable.add ( null );
            }
        }
    }
    result.count = filteredItems.size();
    result.values = filteredItems;
} else {
    synchronized (this) {
        prodDrawable.clear ();
        prodDrawable.addAll ( drawable );
        result.values = originalList;
        result.count = originalList.size();
    }
}

答案 1 :(得分:0)

这是解决方案....这可以帮助任何人

public class Listadapter extends ArrayAdapter<HashMap<String, String>> {

    ArrayList<HashMap<String, String>> originalList;
    ArrayList<HashMap<String, String>> prodlist;
    private ProductFilter filter;
    ArrayList<Drawable> prodDrawable;

    public Listadapter(Context context, int textViewResourceId,
            ArrayList<HashMap<String, String>> Strings) {
        super(context, textViewResourceId, Strings);
        this.prodlist = new ArrayList<HashMap<String, String>>();
        this.prodlist.addAll(productList);
        this.originalList = new ArrayList<HashMap<String, String>>();
        this.originalList.addAll(productList);
        this.prodDrawable = new ArrayList<Drawable>();
        this.prodDrawable.addAll(drawable);
    }

    @Override
    public Filter getFilter() {
        if (filter == null) {
            filter = new ProductFilter();
        }
        return filter;
    }

    private class ViewHolder {
        TextView txtprodName;
        TextView txtcategory;
        TextView txtOfferDate;
        ImageView ProductImage;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        Log.v("ConvertView", String.valueOf(position));
        if (convertView == null) {

            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.product_list_item, null);

            holder = new ViewHolder();
            holder.txtprodName = (TextView) convertView
                    .findViewById(R.id.txtprodName);
            holder.txtcategory = (TextView) convertView
                    .findViewById(R.id.txtcategory);
            holder.txtOfferDate = (TextView) convertView
                    .findViewById(R.id.txtOfferDate);
            holder.ProductImage = (ImageView) convertView
                    .findViewById(R.id.ProductImage);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        HashMap<String, String> hm = prodlist.get(position);

        holder.txtprodName.setText(hm.get(TAG_PRODUCT_NAME));
        holder.txtcategory.setText(hm.get(TAG_CATEGORY_NAME));
        holder.txtOfferDate.setText(hm.get(TAG_OFFER_START_TIME));

        /*
         * if (drawable.get(position) != null)
         * holder.ProductImage.setImageDrawable(drawable.get(position));
         * else
         * holder.ProductImage.setImageResource(R.drawable.nopic_place);
         */

        try {
            holder.ProductImage
                    .setImageDrawable(prodDrawable.get(position));
        } catch (Exception exception) {
            holder.ProductImage.setImageResource(R.drawable.nopic_place);
        }
        return convertView;
    }
    private class ProductFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            constraint = constraint.toString().toLowerCase();
            FilterResults result = new FilterResults();
            if (constraint != null && constraint.toString().length() > 0) {
                ArrayList<HashMap<String, String>> filteredItems = new ArrayList<HashMap<String, String>>();
                prodDrawable.clear();
                for (int i = 0, l = originalList.size(); i < l; i++) {
                    HashMap<String, String> p = originalList.get(i);
                    if (p.toString().toLowerCase().contains(constraint)) {
                        filteredItems.add(p);
                        try {
                            prodDrawable.add(drawable.get(i));
                        } catch (Exception exception) {
                            prodDrawable.add(null);
                        }
                    }
                }
                result.count = filteredItems.size();
                result.values = filteredItems;
            } else {
                synchronized (this) {
                    prodDrawable.clear();
                    prodDrawable.addAll(drawable);
                    result.values = originalList;
                    result.count = originalList.size();
                }
            }
            return result;
        }
        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            // TODO Auto-generated method stub
            prodlist = (ArrayList<HashMap<String, String>>) results.values;
            notifyDataSetChanged();
            clear();
            for (int i = 0, l = prodlist.size(); i < l; i++)
                add(prodlist.get(i));
            notifyDataSetInvalidated();
        }

    }

}