Question

I want to load unique image for each gridview element but it loads only last image for all elements of gridview. How can I solve it ? My example images names are like a50.jpg

where 5 is the number of my category and 0 is a number of element in this category so when you look at this you will see that i want to take walue of category and load all images for those category elements.

Here is my code:

public class TowarAdapter extends ArrayAdapter<Towar> {
    private List<Towar> items;
    private Activity context;

    public TowarAdapter(Activity context, int resource, List<Towar> items) {
        super(context, resource);

        this.items = items;
        this.context = context;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

    @Override
    public Towar getItem(int position) {
        // TODO Auto-generated method stub
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    static class ViewHolder {
        ImageView ivTowar;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder view;

        LayoutInflater inflator = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            view = new ViewHolder();
            convertView = inflator.inflate(R.layout.element, null);

            view.ivTowar = (ImageView) convertView.findViewById(R.id.ivTowar);

            convertView.setTag(view);
        }
        else {
            view = (ViewHolder) convertView.getTag();
        }

        for (int i = 0; i < items.size(); i++) {
            String s = Integer.valueOf(items.get(position).Kat_id).toString();
            int resourceId = context.getResources().getIdentifier("a" + s + i,
                    "drawable", context.getPackageName());
            view.ivTowar.setImageResource(resourceId);
        }
        return convertView;
    }
}

Towar.class:

public class Towar {
    public int Tow_id;
    public int Kat_id;
    public String Tow_nazwa;
    public String Tow_opis;
    public String Tow_producent;
    public String Tow_nr_czesci;
    public float Tow_cena;
    public int Tow_ilosc;
    public String Tow_specyfikacja;
    public Kategoria kategoria;

    public Towar() {

    }

    setters and
    getters

}

No correct solution

OTHER TIPS

You can use the index ie position and get the item from the list.

Store all the ids of images in a list or a Array. Pass the list or array to the constructor of adapter class.

int[] myImageList = new int[]{R.drawable.thingOne, R.drawable.thingTwo};

Pass the array to the constuctor of adapter class

in[] imgid;
public TowarAdapter(Activity context, int resource, int[] imgid)        
{
   super(context, resource);
  this.imgid = imgid

}  
public int getCount() {
// TODO Auto-generated method stub
return imgid.length();
}

Then in getView

 view.ivTowar.setImageResouce(imgid[i]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top