• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Gridview Tutorial; error

rickynl

Newbie
Aug 23, 2010
12
0
Hello, this is my first post.
Im Rick, but ill introduce myself later in that part of the forum,
i just started trying to learn to develop android apps, so i hope someone can be so nice to help me, it would be appreciated.

I am trying to do the gridview tutorial from developer.android.com

I've been busy with it for quite a while now but i can't figure out why it wont work.....

This is my 3rd app (from a tutorial, the hello tut's...)

First i had a lot of errors but after doing some Google-ing, i cleared those away by putting // in front of import android.R;
Like this //import android.R;

But now when i try to run my app, it tells me that my project still contains errors, and that i need to fix them.

I've searched but i cant find the error (eclipse wont show them either...)

All i did was instead of using the example images is use my own....
But i dont see why that should make the difference, all i modified was the name thats it.
Those image names are given on the very last part of the code if you want to check it out.

Also, the images are in the drawable folder!
and it is setup as an android app....


Code:
package com.rickynldev.hellogridview;

//import android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
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(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();
            }
        });
    }


    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.foto1, R.drawable.foto2, 
                R.drawable.foto3, R.drawable.foto4,
                R.drawable.foto5, R.drawable.foto6,
                R.drawable.foto7, R.drawable.foto8,
                R.drawable.foto9, R.drawable.foto10,
                R.drawable.foto11, R.drawable.foto12,
                R.drawable.foto13, R.drawable.foto14,
                R.drawable.foto15, R.drawable.foto16,
                R.drawable.foto17, R.drawable.foto18,
                R.drawable.foto19, R.drawable.foto20,
                R.drawable.foto21, R.drawable.foto22
        };
    }
    
}

And here is the main.xml file if needed...

Code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>
 
From what I gather, you're saying that Eclipse won't run your program because it says there are errors, but it doesn't show you any errors in your files.

I've actually run across this incredibly annoying bug a couple times, and thankfully the second time around I realized if you just remove the project from your workspace (remove, don't delete contents on disk) and then just import the project into Eclipse again, it should show up without errors (assuming there actually are no errors).
 
  • Like
Reactions: rickynl
Upvote 0
Thanks!
That immediately fixed it.
However my pictures dont seem to work only reason i can figure out is because they're too large?

However when i use the sample pictures that are provided it does work
Thank you!

I had the same problem, but also, where and how did you go about getting all the imports at the top? It didn't say anything about adding those. So a lot of my errors were from that.

I don't really know anything about Java, so this is a little confusing to me.
 
Upvote 0
From the tutorial page for Grid View:

2. Find some photos you'd like to use, or download these sample images. Save the image files into the project's res/drawable/ directory.

This means:

You must download them to the directory (listed above) that is in the Package Explorer window of the Eclipse Application. The Package Explorer is the window that contains a large directory tree (i.e., the one that contains all of the resources, including your code). It should be at the far left of your screen, and if you look at the TAB's running across the top, is should be the first TAB titled "Package Explorer".

Wherever you have put them, you must now; SelectThem, HighlightAllofThem, then right-click to "copy", and then paste them into "each of the availble /drawable-... directories" you find at the bottom of the path: HelloGridView/res directory in the Package Explorer window of the Eclipse Application.

Do not inlcude the folder, just copy the individual files into the directory (highlight res/drawable-hdpi, for instance then...), by pressing right-click to "paste" them. Then paste a copy of them into each of the other remaining "/drawable-..." directories. These directories define the pictures for each of several (3 on mine) different screen densities. .hdpi (high) .mdpi (medium) and ldpi (low). Depending on what emulator or target version you choose; the presense or absence of the pictures in the proper directory, will obviously cause problems.

Good Luck!

David
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones