【问题标题】:Setting image for imageview within listview yields odd results在 listview 中为 imageview 设置图像会产生奇怪的结果
【发布时间】:2013-08-19 17:55:37
【问题描述】:

我有一个自定义列表视图,它在列表行布局中有一个 SmartImageView (http://loopj.com/android-smart-image-view/)

我正在尝试将每一行上的每个 SmartImageView 设置为一张图片,下面是我的列表适配器。

private class MyListAdapter extends ArrayAdapter<ListItem> {

        private ArrayList<ListItem> items;

        public MyListAdapter(Context context, int textViewResourceId, ArrayList<ListItem> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.item_row_layout, null);
                }
                ListItem o = items.get(position);
                if (o != null) {
                        TextView title = (TextView) v.findViewById(R.id.ptitle);
                        TextView site = (TextView) v.findViewById(R.id.site);
                        TextView price = (TextView) v.findViewById(R.id.price);
                        SmartImageView img = (SmartImageView) findViewById(R.id.smallimage);

                        Typeface font = Typeface.createFromAsset(getAssets(), "Asap-Regular.ttf");  
                        site.setTypeface(font);
                        title.setTypeface(font); 
                        price.setTypeface(font);

                        if (title != null) {
                            title.setText(o.getTitle());                            
                        }
                        if (site != null) {

                              site.setText("Supplier Name");

                        }
                        if (price != null) {
                            price.setText("£"+ String.valueOf(o.getPrice()));                            
                        }
                        if (img != null){
                            String url = o.getImage();
                            img.setImageUrl(url);
                        }





                }
                return v;
        }

    }

所有其他东西都设置好了,例如标题、价格等,但是图像设置不正确。

如果列表项超过一个,则顶部项将获取最后一项图像,其余项没有图像集。 如果列表中有一项,则根本不会设置图像。

如果您需要更多代码或信息,请告诉我!

【问题讨论】:

  • 设置 img.setBackgroundDrawable(null);当 img 为 null 时应该可以解决您的问题。
  • @Tarun 的评论是正确的解决方案,而不是市场上正确的答案。这与回收有关。它正在重用视图。
  • @dannyroa 下面的答案对我有用,所以被标记为这样
  • @ZacPowell:是的,它有效,但它不是正确的做法。

标签: java android android-listview android-imageview


【解决方案1】:

您应该尝试通过调用为您的 SmartImageView 执行 findViewById

SmartImageView img = (SmartImageView) v.findViewById(R.id.smallimage);

这样,您将获得列表的每个视图中的 SmartImageView,否则,它不会获取列表视图中特定于该行的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 2014-03-05
    • 2020-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多