1

私は Android の初心者ですが、カスタムlistviewフィルタリングを作成しようとしましたが、何とか機能しました。私が抱えている唯一の問題はArrayList、画像を表示していて、リストを検索すると機能するが、画像が間違って表示されることです。これを説明することはできませんが、何とかしてくれると思いました。

とにかくここにカスタムがありArrayAdaptorます:

public class Listadapter extends ArrayAdapter<HashMap<String, String>> {
    ArrayList<HashMap<String, String>> originalList;
    ArrayList<HashMap<String, String>> productlist;
    private ProductFilter filter;

    public Listadapter(Context context, int textViewResourceId,
            ArrayList<HashMap<String, String>> Strings) {
        super(context, textViewResourceId, Strings);
        this.productlist = new ArrayList<HashMap<String, String>>();
        this.productlist.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;
        }*/

    public int getCount() {
        return productList.size();
    }

    public long getItemId(int position) {
        // return productList.indexOf(getItem(position));
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //ViewHolder holder = null;
        TextView txtprodName, txtcategory, txtOfferDate;
        ImageView ProductImage;
        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();
        txtprodName = (TextView) convertView
                .findViewById(R.id.txtprodName);
        txtcategory = (TextView) convertView
                .findViewById(R.id.txtcategory);
        txtOfferDate = (TextView) convertView
                .findViewById(R.id.txtOfferDate);
        ProductImage = (ImageView) convertView
                .findViewById(R.id.ProductImage);
        //convertView.setTag(holder);

        /*} else {
                //holder = (ViewHolder) convertView.getTag();
            }*/
        HashMap<String, String> hm = productList.get(position);

        // txtUserName.setText(lstUsers.get(position).getFirst_Name()+" "+lstUsers.get(position).getLast_Name());
        txtprodName.setText(hm.get(TAG_PRODUCT_NAME));
        txtcategory.setText(hm.get(TAG_CATEGORY_NAME));
        txtOfferDate.setText(hm.get(TAG_OFFER_START_TIME));

        if (drawable.get(position) != null)
            ProductImage.setImageDrawable(drawable.get(position));
        else
            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++) {
                // ArrayList<HashMap<String, String>> p =
                // originalList.get(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
        productList = (ArrayList<HashMap<String, String>>) results.values;
        notifyDataSetChanged();
        clear();
        for (int i = 0, l = productList.size(); i < l; i++)
            add(productList.get(i));
        notifyDataSetInvalidated();
    }
}

以下は結果です

ここに画像の説明を入力 ここに画像の説明を入力

4

3 に答える 3

0

このリファレンスをご覧ください。

これは解決され、要件と同じように機能します。

検索フィルタリングリストビュー

このリンクに目を通し、まだ問題がある場合はお知らせください。

編集:

dummy_drawable ArrayListと同じものを維持し、ベースとしてのみ保持し、アダプタで使用する必要drawable ArrayListがあります。drawable ArrayListdummy_drawable

dummy_drawable ArrayListまた、メソッドからこれを更新しますFilterResults。このような:

dummy_drawable.clear();

for (int i = 0, l = originalList.size(); i < l; i++) {
        // ArrayList<HashMap<String, String>> p =
        // originalList.get(i);
        HashMap<String, String> p = originalList.get(i);
        if (p.toString().toLowerCase().contains(constraint)){
              filteredItems.add(p);
              dummy_drawable.add(drawable.get(i));                 
        }
}

ありがとう。

于 2012-12-27T07:30:26.457 に答える
0

imageview を使用した検索データの例はたくさんあります。

ここにいくつかの良いリンクがあります。最初にコードを読んで理解してから、要件に従って実装してください。

例 1

例 2

于 2012-12-27T09:12:08.520 に答える
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:21:30.423 に答える