Mochammad Irsyad Mochammad Irsyad - 2 months ago 22
Java Question

GridView show wrong data

My gridview will show right data if image view layout width 100 and layout height 100

1 2

3 4

5 6

7

butttt... if i use imageview with

layout width 200 and layout height 200

that will show like this

1 2

3 4

5 6

1

can you explain me? thanks

this my code

this main activity gridview

<GridView
android:id="@+id/list_topic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="5dp"
android:numColumns="2"
android:verticalSpacing="5dp"></GridView>


this custom layout for grid

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/gridview_image"
android:layout_width="150dp"
android:layout_height="match_parent"
android:scaleType="centerCrop" />

<TextView
android:id="@+id/gridview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/gridview_image"
android:background="#99000000"
android:gravity="center"
android:padding="5dp"
android:textColor="#ffffff"
android:textSize="12sp"/>


and this customadapter gridview

class customadapter extends BaseAdapter

private Context mContext;
private ArrayList<Bitmap> mImage;
private ArrayList<String> mTopic;

public GridViewAdapter(Context c, ArrayList<String> topic, ArrayList<Bitmap> image){
mContext = c;
this.mImage = image;
this.mTopic = topic;
}

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

public Object getItem(int position) {
return mTopic.get(position);
}

public long getItemId(int position) {
return 0;
}

public View getView(int position, View convertView, ViewGroup viewGroup) {

LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridViewAndroid;

if (convertView == null) {
gridViewAndroid = new View(mContext);
gridViewAndroid = inflater.inflate(R.layout.grid_layout, null);

TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.gridview_text);
ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.gridview_image);

textViewAndroid.setText(mTopic.get(position));
imageViewAndroid.setImageBitmap(mImage.get(position));

} else {
gridViewAndroid = (View) convertView;
}
return gridViewAndroid;
}

Answer

If convertView was not null (it was recycled), you were not setting any value and you just returned the same view with same image. GridView is recycling views. If you set height to 100, all 7 views were inflated. If you set hight to 200, only 6 were inflated and when you scrolled down, the first two were recycled and you have to set them values that corespond to their new position.

public View getView(int position, View convertView, ViewGroup viewGroup) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View gridViewAndroid;

    if (convertView == null) {
       gridViewAndroid = new View(mContext);
       gridViewAndroid = inflater.inflate(R.layout.grid_layout, null);
    } else {
       gridViewAndroid = (View) convertView;
    }

    TextView textViewAndroid = (TextView) gridViewAndroid.findViewById(R.id.gridview_text);
    ImageView imageViewAndroid = (ImageView) gridViewAndroid.findViewById(R.id.gridview_image);

    textViewAndroid.setText(mTopic.get(position));
    imageViewAndroid.setImageBitmap(mImage.get(position));

    return gridViewAndroid;
}