Jayizzle
Jayizzle

Reputation: 532

Android Gallery only showing three pictures

Pic of app here: http://imgur.com/6A92Yl6

So I have a gallery set up that upon a click-through allows the user to select a picture from the view and then see the picture and their corresponding toast with it.

The problem is that there are only THREE images that pop up. I don't know why and theres no variable that limits the gallery so I am at a loss for words.

My main:

public class MainActivity<View> extends Activity 
{

 ImageView selectedImage;  
 private Integer[] mImageIds = {
            R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.image7,
            R.drawable.image8,
            R.drawable.image9,
            R.drawable.image10
    };
@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

         Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    selectedImage=(ImageView)findViewById(R.id.imageView1);
    gallery.setSpacing(1);
    gallery.setAdapter(new GalleryImageAdapter(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(MainActivity.this, "THIS TEXT SHOULD NEVER SHOW = " + position, Toast.LENGTH_SHORT).show();
            // show the selected Image
            selectedImage.setImageResource(mImageIds[position]);
        }

        @Override
        public void onItemClick(AdapterView<?> parent, android.view.View v, int position, long id) {
            Toast.makeText(MainActivity.this, "Picture Number: " + position, Toast.LENGTH_SHORT).show();
            // show the selected Image
            selectedImage.setImageResource(mImageIds[position]);

        }
    });
}
}

MY XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Gallery
    android:id="@+id/gallery1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="350dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="100dp"
    android:layout_weight="0.13"
    android:src="@drawable/image1" />

</LinearLayout>

Upvotes: 0

Views: 257

Answers (2)

Anil Jadhav
Anil Jadhav

Reputation: 2158

You need to use Adapter:

  1. Change call of adapter to:

    gallery.setAdapter(new GalleryImageAdapter(this, mImageIds));

  2. Add/Update GalleryImageAdapter.java:

    import java.util.List;
    
    import android.app.Activity;
    import android.graphics.drawable.Drawable;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Gallery;
    import android.widget.ImageView;
    
    public class GalleryImageAdapter extends BaseAdapter {
    
    private Activity context;
    
    private static ImageView imageView;
    
    private List<Drawable> plotsImages;
    
    private static ViewHolder holder;
    
    public GalleryImageAdapter(Activity context, List<Drawable> plotsImages) {
    
    this.context = context;
    this.plotsImages = plotsImages;
    
    }
    
    @Override
    public int getCount() {
        return plotsImages.size();
    }
    
    @Override
    public Object getItem(int position) {
        return null;
    }
    
    @Override
    public long getItemId(int position) {
        return 0;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
      if (convertView == null) {
    
        holder = new ViewHolder();
    
        imageView = new ImageView(this.context);
    
        imageView.setPadding(3, 3, 3, 3);
    
        convertView = imageView;
    
        holder.imageView = imageView;
    
        convertView.setTag(holder);
    
    } else {
    
        holder = (ViewHolder) convertView.getTag();
    }
    
    holder.imageView.setImageDrawable(plotsImages.get(position));
    
    holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    holder.imageView.setLayoutParams(new Gallery.LayoutParams(150, 90));
    
    return imageView;
    }
    
    private static class ViewHolder {
        ImageView imageView;
    }
    
    }
    

Upvotes: 1

Rohan
Rohan

Reputation: 74

I guess you forgot to populate the adapter with mImageIds when creating the new GalleryImageAdapter.

Upvotes: 1

Related Questions