0

I have a custom listview which within the list row layout has a SmartImageView (http://loopj.com/android-smart-image-view/)

I am trying to set each SmartImageView on each row to a picture, below is my list adapter.

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

    }

All other things get set alright i.e title, price etc however the images do not get set correctly.

If there is more than one list item than the top item will get the last items image, and the rest has no image set. While if there is one item in the list it does not get a image set at all.

If you need any more of the code or information do let me know!

4
  • Setting img.setBackgroundDrawable(null); when img is null should solve your problem.
    – Tarun
    Aug 19, 2013 at 18:06
  • @Tarun's comment is the correct solution not the Answer that was market as correct below. This has something to do with recycling. It is reusing the views.
    – dannyroa
    Aug 19, 2013 at 18:20
  • @dannyroa the answer below worked for me so was marked as such Aug 20, 2013 at 10:04
  • @ZacPowell: Yes it works but it's not the right way of doing it.
    – dannyroa
    Aug 20, 2013 at 20:16

1 Answer 1

0

You should try doig the findViewById for your SmartImageView by calling

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

This way, you get the SmartImageView that is in each view of the list, otherwise, it doesn't grab the one that is specific to that row in your listview.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.