0

Hello everyone I have a problem. I want to do a GridView with a Custom Loyout so i used the layoutInflater and i did this:

private ImageView prima;
private ImageView seconda;

public View getView(int position, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null) {

            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.icon, null);

            Display display = getWindowManager().getDefaultDisplay();
            int width = display.getWidth();

            prima = (ImageView) v.findViewById(R.id.imageView1);
            prima.getLayoutParams().height = width / 3;
            prima.getLayoutParams().width = width / 3;
            seconda = (ImageView) v.findViewById(R.id.imageView2);
            seconda.getLayoutParams().height = width / 3;
            seconda.getLayoutParams().width = width / 3;

            v.setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
            v.setPadding(0, 0, 0, 0);
        } else {
            v = convertView;
        }

        prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....
        return v;

    };

When i run my application the image are arranged random and there are many black space with no images.

What i did wrong ?

Matteo Cardellini
  • 876
  • 2
  • 17
  • 41

2 Answers2

2

Your View and convertView are not linked. Try with ViewHolder concept like this:

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder v;
        if (convertView == null) {

            LayoutInflater li = getLayoutInflater();
            convertView  = li.inflate(R.layout.icon, null);

            Display display = getWindowManager().getDefaultDisplay();
            int width = display.getWidth();

            v = new ViewHolder();
            v.prima = (ImageView) convertView .findViewById(R.id.imageView1);
            v.prima.getLayoutParams().height = width / 3;
            v.prima.getLayoutParams().width = width / 3;
            v.seconda = (ImageView) convertView .findViewById(R.id.imageView2);
            v.seconda.getLayoutParams().height = width / 3;
            v.seconda.getLayoutParams().width = width / 3;

            convertView .setLayoutParams(new GridView.LayoutParams(width / 3, width / 3));
            convertView .setPadding(0, 0, 0, 0);
            convertView.setTag(v);
        } else {
            v = (ViewHolder)convertView.getTag();
        }

        v.prima.setImageResource(mThumbIds[position]); //mThumbIds[] is an array with R.drawable.vip_0_mini, R.drawable.a_1, R.drawable.b_2, R.drawable.c_3 .....

         //v.seconda
        return convertView;

    };

class ViewHolder{
    ImageView prima;
    ImageView seconda;
}
Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0

You forgot to apply images to

    seconda = (ImageView) v.findViewById(R.id.imageView2);

    prima.setImageResource(....);
Vipul
  • 27,808
  • 7
  • 60
  • 75