1

i have a problem with a image gridView. I put all the images in an array, but when the grid is displayed the images aren’t in the order that I decided. Here is my code taken from various tutorials.

The main activity:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Clicca sull'immagine per ingrandire"
    android:id="@+id/textView2"
    android:layout_gravity="center_horizontal"
    android:textAlignment="center"
    android:paddingBottom="10sp"
    android:textColor="@color/white"
    android:textSize="@dimen/text_size"/>

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/grid_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="auto_fit"
    android:columnWidth="300dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >
</GridView>

Related java code:

package ...;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

public class  extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_);

    GridView gridView = (GridView) findViewById(R.id.grid_view);

    // Instance of ImageAdapter Class
    gridView.setAdapter(new ImageAdapter(this));

    /**
     * On Click event for Single Gridview Item
     * */
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v,
                                int position, long id) {

            // Sending image id to FullScreenActivity
            Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
            // passing array index
            i.putExtra("id", position);
            startActivity(i);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

The Image Adapter:

package com.example.valeria.Flexibilia_abbigliamento_per_danze_caraibiche_by_DDLAB;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

// Keep all Images in array
public Integer[] mThumbIds = {
        R.drawable.max27a1ridotta, R.drawable.max27b1ridotta,
        R.drawable.max27c1ridotta, R.drawable.max29a1ridotta,
        R.drawable.max29b1ridotta, R.drawable.max30a1ridotta,
        R.drawable.max30b1ridotta, R.drawable.max33a1ridotta,
        R.drawable.max33b1ridotta, R.drawable.max36a1ridotta,
        R.drawable.max38a1ridotta, R.drawable.max38b1ridotta,
        R.drawable.max281ridotta, R.drawable.max311ridotta,
        R.drawable.max321ridotta, R.drawable.max341ridotta,
        R.drawable.max351ridotta, R.drawable.max371ridotta,
        };

// Constructor
public ImageAdapter(Context c) {
    mContext = c;
}

@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
    } else {
        imageView = (ImageView) convertView;
    }

    return imageView;
}
}

The gridView display the correct number of items, but the images are randomized. I am new in android and in programming, and is possible that taking examples from various sources I put too much code. Where did I go wrong?

Thanks and sorry for my English…

1 Answer 1

7

The problem is not in the order of images, the problem is in the way you work with convertView. even if it is not null you have to fill it, otherwise you will get a cached View for different position. So this code should solve the problem:

        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setLayoutParams(new GridView.LayoutParams(350, 300));
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbIds[position]);
        return imageView;

And just for the future I would suggest you to read about ViewHolder patern

2
  • Ok, it work! I have already watched for the viewHolder, but there is something i don't understand... i'm slow. Thanks! Commented May 19, 2015 at 8:02
  • @Marcellino247, you're welcome, as for ViewHolder it's basically is a storage that has reference to Views (it's more convenient when child views have complex hierarchy and several views to be set up), so you won't have to do binding once again (findViewById), you will just have to set appropriate data to these Views. Also if the answer is helpful could you please mark it as correct?
    – Chaosit
    Commented May 19, 2015 at 12:55

Your Answer

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

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