3

I'm trying to get into android development and I'm going through the tutorials, however I'm really stuck on the HelloGridView tutorial and I'm surrounded by apple developers, so have nobody with any android experience to ask!

I've attempted to do the HelloGridView tutorial and I get 17 Errors but cant see where am I going wrong?!? I have a feeling I'm missing a library or something?!?

As I've got so many errors I cant solve I have taken a screenshot: http://users.cscs.wmin.ac.uk/~w1128683/help/Untitled.jpeg

The problems lie in the HelloGridView and ImageAdapter classes:

HelloGridView class:

    package com.example.HelloGridView;

    import android.app.Activity;
    import android.os.Bundle;

    public class HelloGridView extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));

            gridview.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
                }
            });
        }

ImageAdapter class:

    package com.example.HelloGridView;

    public class ImageAdapter extends BaseAdapter {
        private Context mContext;

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

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

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = {
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7,
            R.drawable.sample_0, R.drawable.sample_1,
            R.drawable.sample_2, R.drawable.sample_3,
            R.drawable.sample_4, R.drawable.sample_5,
            R.drawable.sample_6, R.drawable.sample_7
    };
    }

If anyone could tell me where I'm going wrong or at least point me in the right direction, it would be much appreciated!

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Exile
  • 9,163
  • 4
  • 23
  • 22

2 Answers2

7

Here is the fixed setup, next time you need to do the imports for each object:

Your HelloGridView

package com.example.HelloGridView;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class HelloGridView extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new ImageAdapter(getApplicationContext()));

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {
                Toast.makeText(HelloGridView.this, "" + position,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Your Base Adapter

package com.example.HelloGridView;

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;

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

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

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

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

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    private Integer[] mThumbIds = { R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7, R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6, R.drawable.sample_7 };
}
Jacob Malliet
  • 525
  • 3
  • 11
  • ahh I thought it was something like that. Next time I'll try and use common scene instead of following the tutorial to the letter! Thanks for your help Jacob its much appreciated! – Exile Jul 18 '10 at 16:51
  • 5
    No Problem, like bevor said below, just hit ctrl+shift+o and eclipse will do all necessary imports – Jacob Malliet Jul 18 '10 at 16:53
4

Eclipse just didn't import all the important parts. Press STRG + Shift + O in Eclipse while your HelloGridView class is focused. Then create the other class ImageAdapter and it will work.

Bevor
  • 8,396
  • 15
  • 77
  • 141
  • However, I find that Eclipse automatically adds `import android.R;` when auto-import is done (Juno Service Release 1, Android 4.1.2), and this has to be deleted or errors are raised on lines having `R.id...` as argument. – brannerchinese Dec 12 '12 at 18:21
  • You can auto import in android studio as well - [android studio auto-import](http://stackoverflow.com/questions/16615038/what-is-the-shortcut-to-auto-import-all-in-android-studio) – lostAtSeaJoshua Nov 02 '16 at 20:03