0

我正在过滤列表视图,我在其中过滤来自本地主机的数据和图像...在其中过滤了数据但未过滤图像。当我搜索该数据时,但图像来自其原始位置意味着图像来自其原始位置......

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();

        }

    }

}
4

2 回答 2

0

您也应该按以下方式过滤可绘制列表:

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

ArrayList <Drawable> prodDrawable;

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

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

在getView方法中使用新列表(而不是可绘制列表):

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

最后,在Filter中,您也应该过滤图像列表,更改:

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();
    }
}

对此:_

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();
    }
}
于 2013-01-04T08:54:36.077 回答
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();
        }

    }

}
于 2013-01-04T12:18:15.047 回答