dw19
dw19

Reputation: 73

I am getting error when trying to create an image gallery in android using ViewPager and GridView

Here I have created an ImageAdapter class by extending PagerAdapter

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;



public class ImageAdapter extends PagerAdapter {
    private Context mContext;

    // Keep all Images in array
    public Integer[] mThumbIds = new Integer[] {
            R.drawable.a, R.drawable.b,
            R.drawable.c, R.drawable.d,
            R.drawable.e, R.drawable.f,
            R.drawable.g, R.drawable.h,
            R.drawable.i, R.drawable.j,
            R.drawable.k, R.drawable.l,
            R.drawable.m, R.drawable.n,
            R.drawable.o, R.drawable.p,
            R.drawable.q, R.drawable.r,
            R.drawable.t, R.drawable.u,
            R.drawable.v
    };

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

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

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setPadding(8, 8, 8, 8);
        imageView.setImageResource(mThumbIds[position]);
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
}

This is working fine, with just some redundant warnings, that I don't care about right now.

And here is Fragment class that will display images stored in ImageAdapter class

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.view.ViewGroup;
import android.widget.GridView;





public class PhotosFragment extends Fragment {

    public PhotosFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_photos, container, false);

        GridView gridView = (GridView)rootView.findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(getActivity()));


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

        // Instance of ImageAdapter Class


        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {

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

And then here is FullImageActivity class for displaying images in fullscreen

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;


public class FullImageActivity extends Activity{

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.full_image);

        // get intent data
        Intent i = getIntent();

        // Selected image id
        int position = i.getExtras().getInt("id");
        ImageAdapter imageAdapter = new ImageAdapter(this);

        ViewPager imageView = (ViewPager) findViewById(R.id.full_image_view);
        imageView.addView(imageAdapter.mThumbIds[position]);
    }




}

I am getting error in PhotoFragment class-setAdapter in GridView can not be applied to .ImageAdapter

And in FullImageActivity class-addView in ViewGroup can not be applied to .Integer

And here are the layout xml files

full_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <android.support.v4.view.ViewPager android:id="@+id/full_image_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

fragment_photo

<?xml version="1.0" encoding="utf-8"?>
<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="90dp"
    android:horizontalSpacing="10dp"
    android:verticalSpacing="10dp"
    android:gravity="center"
    android:stretchMode="columnWidth" >

</GridView>

Upvotes: 0

Views: 340

Answers (1)

NasaGeek
NasaGeek

Reputation: 2198

As an explanation for the errors you are receiving:

GridView's setAdapter method only accepts ListAdapters, not PagerAdapters. PagerAdapters are intended to be used with ViewPagers, hence the name "PagerAdapter". You'll probably want your ImageAdapter to extend something like an ArrayAdapter instead.

Somewhat related to the above explanation, you also need an adapter to back your ViewPager. I'm honestly not sure what the behavior of a ViewPager is after calling addView, but I doubt it's behavior you're looking for. You also are giving addView the incorrect parameter regardless. You are giving it an Integer (as the error states) from mThumbIds, but it expects a view.

You might benefit from reading the following post if you really have your heart set on using addView with your ViewPager: dynamically add and remove view to viewpager

Upvotes: 1

Related Questions