-1

I am getting this error:

Description Resource    Path    Location    Type
Swipeactivity cannot be resolved to a variable  SwipeActivity.java  /Copy of TD GridView/src/com/td/gridview    line 32 Java Problem

code is

int[] icons = MainActivity.ICONS;    

    //get image position

    Intent n = getIntent();
    int position= n.getIntExtra("id", position);

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter(Swipeactivity, icons[position]);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);

problem in this line:

ImagePagerAdapter adapter = new ImagePagerAdapter(Swipeactivity, icons[position]);

Swipeactivity cannot be resolved to a variable

So how do I pass Swipeactivity class to a context variable?

like:

context = swipeactivity;

so can use it like

ImagePagerAdapter adapter = new ImagePagerAdapter(Context , icons[position]);

please see this link it depends on my previous question it will help you to understand


after too many errors I finally got the answer and it's very simply...;)

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

    // Selected image id
    int position = i.getExtras().getInt("id");

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);

this is the simple answer instead of this code:

ImagePagerAdapter adapter = new ImagePagerAdapter(mContext , icons[position]);

this worked.

viewPager.setCurrentItem(position);

now below is my whole app code:

MainActivity.java

package com.td.gridview;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {


    private GridView photoGrid;
    private int mPhotoSize, mPhotoSpacing;
    private ImageAdapter imageAdapter;

    // Some items to add to the GRID
    private static final String[] CONTENT = new String[] 
            { 
            "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean",
            "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Akon",
            "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary", "Micheal Buble", "Britney Spears",
            "Hilary", "Micheal Buble", "Akon", "Justin Bieber", "AlRight", "Big Sean", "Britney Spears", "Hilary",
            "Micheal Buble" 
            };
    static final int[] ICONS = new int[] 
            { 
            R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin,
            R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary,
            R.drawable.cover_mb, R.drawable.cover_akon, R.drawable.cover_justin, R.drawable.cover_alright,
            R.drawable.cover_big_sean, R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb,
            R.drawable.cover_britney, R.drawable.cover_hilary, R.drawable.cover_mb, R.drawable.cover_akon,
            R.drawable.cover_justin, R.drawable.cover_alright, R.drawable.cover_big_sean, R.drawable.cover_britney,
            R.drawable.cover_hilary, R.drawable.cover_mb 
            };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        // get the photo size and spacing
        mPhotoSize = getResources().getDimensionPixelSize(R.dimen.photo_size);
        mPhotoSpacing = getResources().getDimensionPixelSize(R.dimen.photo_spacing);

        // initialize image adapter
        imageAdapter = new ImageAdapter();

        photoGrid = (GridView) findViewById(R.id.albumGrid);

        //start sent image to full screen             

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

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

        // set image adapter to the GridView
        photoGrid.setAdapter(imageAdapter);

        // get the view tree observer of the grid and set the height and numcols dynamically
        photoGrid.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (imageAdapter.getNumColumns() == 0) {
                    final int numColumns = (int) Math.floor(photoGrid.getWidth() / (mPhotoSize + mPhotoSpacing));
                    if (numColumns > 0) {
                        final int columnWidth = (photoGrid.getWidth() / numColumns) - mPhotoSpacing;
                        imageAdapter.setNumColumns(numColumns);
                        imageAdapter.setItemHeight(columnWidth);

                    }
                }
            }
        });
    }

    // ///////// ImageAdapter class /////////////////
    public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private int mItemHeight = 0;
        private int mNumColumns = 0;
        private RelativeLayout.LayoutParams mImageViewLayoutParams;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                    LayoutParams.MATCH_PARENT);
        }

        public int getCount() {
            return CONTENT.length;
        }

        // set numcols
        public void setNumColumns(int numColumns) {
            mNumColumns = numColumns;
        }

        public int getNumColumns() {
            return mNumColumns;
        }

        // set photo item height
        public void setItemHeight(int height) {
            if (height == mItemHeight) {
                return;
            }
            mItemHeight = height;
            mImageViewLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, mItemHeight);
            notifyDataSetChanged();
        }

        public Object getItem(int position) {
            return position;
        }

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

        public View getView(final int position, View view, ViewGroup parent) {

            if (view == null)
                view = mInflater.inflate(R.layout.photo_item, null);

            ImageView cover = (ImageView) view.findViewById(R.id.cover);
            TextView title = (TextView) view.findViewById(R.id.title);

            cover.setLayoutParams(mImageViewLayoutParams);

            // Check the height matches our calculated column width
            if (cover.getLayoutParams().height != mItemHeight) {
                cover.setLayoutParams(mImageViewLayoutParams);
            }

            cover.setImageResource(ICONS[position % ICONS.length]);
            title.setText(CONTENT[position % CONTENT.length]);

            return view;
        }
    }

}

SwipeActivity.java

    package com.td.gridview;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    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 SwipeActivity extends Activity 
    {   

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

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

        // Selected image id
        int position = i.getExtras().getInt("id");

    ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
    ImagePagerAdapter adapter = new ImagePagerAdapter();
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(position);
  }

  private class ImagePagerAdapter extends PagerAdapter 
  {
      int[] icons = MainActivity.ICONS;    

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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) 
    {
      Context context = SwipeActivity.this;
      ImageView imageView = new ImageView(context);
//      int padding = context.getResources().getDimensionPixelSize(
//          R.dimen.padding_large);
//      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
      imageView.setImageResource(icons[position]);
      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

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

activity_main.xml

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/albumGrid"
    style="@style/PhotoGrid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:columnWidth="@dimen/photo_size"
    android:horizontalSpacing="@dimen/photo_spacing"
    android:numColumns="auto_fit"
    android:padding="4dp"
    android:scrollbars="none"
    android:stretchMode="columnWidth"
    android:verticalSpacing="@dimen/photo_spacing" />

photo_item.xml

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

<ImageView
    android:id="@+id/cover"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/cover"
    android:background="#70000000"
    android:padding="6dp" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="@color/white"
        android:textSize="12sp"
        android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>

swipe_view.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />
Community
  • 1
  • 1
user3739970
  • 591
  • 2
  • 13
  • 28
  • 1
    try Using `SwipeActivity.this` instead of `SwipeActivity` – insomniac Jun 16 '14 at 11:38
  • **after chang:** ` ImagePagerAdapter adapter = new ImagePagerAdapter(SwipeActivity.this, icons[position]); ` **got this error** `Description Resource Path Location Type The constructor SwipeActivity.ImagePagerAdapter(SwipeActivity, int) is undefined SwipeActivity.java /Copy of TD GridView/src/com/td/gridview line 32 Java Problem ` – user3739970 Jun 16 '14 at 11:43

2 Answers2

1

As posted here

As the name suggests, its the context of current state of the application/object. It lets newly created objects understand what has been going on. Typically you call it to get information regarding another part of your program (activity, package/application)

The error is that you are trying to pass a class other than the context to the parameter You should try replacing SwipeActivity with SwipeActivity.this or getApplicationContext().

Community
  • 1
  • 1
insomniac
  • 11,146
  • 6
  • 44
  • 55
0

You have a GridView, you tap an image, you see the image inside a swipeView, in order to keep viewing the rest of the images without going back, i'm right?

Assuming that your SwipeActivity does what it has to do (start in the right image using the "id" string you pass), If you need a Context to pass to the adapter, just crate a field in your Activity called mContext, and give it the Activity's "this" value just when you enter to the onCreate method;

class MyActivity extends Activity ...{

Context mContext;

...

@Override
onCreate(Bundle savedInstanceState){


mContext = this;

...

ImagePagerAdapter adapter = new ImagePagerAdapter(mContext , icons[position]);

}
}

In the other hand, if you just want to see just one image at a time and then go back again, using a PageAdapter, would not be the best choice, because it is oriented to see strips of images in a paged structure, instead you could use some kind of simnple imageView container in order to do that.

From my comment:

then use the setCurrentItem(position) to get there
meyo
  • 118
  • 7
  • **please note:** i have a grid view and what i want is when i tap a image my image swipe should starts from selected image. **but** the problem is image swipe is not getting image position so image swipe always starts from 1st image, and i want it to start from selected image. please see my code in my previous question see link in my question above – user3739970 Jun 16 '14 at 13:52
  • I've done so. And after your clarifications I don't really understand why you initialize an adapter with just one image then, modify the constructor and give him the icons[] array, create the structure and then use the setCurrentItem(position) to get there, use the correct context, wich you can find in the above answer. By passing icons[position] you are only giving a pointer to an image, so obviously you cant' extrapolate the position from anywhere. – meyo Jun 16 '14 at 16:32
  • i found solution, check my updated question. now will anybody increase my reputation. why it decreased. – user3739970 Jun 16 '14 at 17:21
  • Your reputation descrased due a lack of comprehension of what you were implementing, the bad structure of the qüestion (the problem you described is not the one you have, you needed several edits to clarify), and the overflow of text in your qüestion (more text does not mean more useful data). the community may understand if you are not a guru, but some accurate definition of the problem is needed always. Also, the solution you "found" is in my last comment. – meyo Jun 17 '14 at 10:31