Gridview Tutorial; error

Last Updated:

  1. rickynl

    rickynl Member This Topic's Starter

    Aug 23, 2010
    Likes Received:
    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

    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 (Text):
    1. package com.rickynldev.hellogridview;
    3. //import android.R;
    4. import;
    5. import android.content.Context;
    6. import android.os.Bundle;
    7. import android.view.View;
    8. import android.view.ViewGroup;
    9. import android.widget.AdapterView;
    10. import android.widget.BaseAdapter;
    11. import android.widget.GridView;
    12. import android.widget.ImageView;
    13. import android.widget.Toast;
    14. import android.widget.AdapterView.OnItemClickListener;
    16. public class HelloGridView extends Activity
    17. {
    18.     /** Called when the activity is first created. */
    19.     @Override
    20.     public void onCreate(Bundle savedInstanceState) {
    21.         super.onCreate(savedInstanceState);
    22.         setContentView(R.layout.main);
    24.         GridView gridview = (GridView) findViewById(;
    25.         gridview.setAdapter(new ImageAdapter(this));
    27.         gridview.setOnItemClickListener(new OnItemClickListener() {
    28.             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
    29.                 Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
    30.             }
    31.         });
    32.     }
    35.     public class ImageAdapter extends BaseAdapter {
    36.         private Context mContext;
    38.         public ImageAdapter(Context c) {
    39.             mContext = c;
    40.         }
    42.         public int getCount() {
    43.             return mThumbIds.length;
    44.         }
    46.         public Object getItem(int position) {
    47.             return null;
    48.         }
    50.         public long getItemId(int position) {
    51.             return 0;
    52.         }
    54.         // create a new ImageView for each item referenced by the Adapter
    55.         public View getView(int position, View convertView, ViewGroup parent) {
    56.             ImageView imageView;
    57.             if (convertView == null) {  // if it's not recycled, initialize some attributes
    58.                 imageView = new ImageView(mContext);
    59.                 imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
    60.                 imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    61.                 imageView.setPadding(8, 8, 8, 8);
    62.             } else {
    63.                 imageView = (ImageView) convertView;
    64.             }
    66.             imageView.setImageResource(mThumbIds[position]);
    67.             return imageView;
    68.         }
    70.         // references to our images
    71.         private Integer[] mThumbIds = {
    73.                 R.drawable.foto1, R.drawable.foto2,
    74.                 R.drawable.foto3, R.drawable.foto4,
    75.                 R.drawable.foto5, R.drawable.foto6,
    76.                 R.drawable.foto7, R.drawable.foto8,
    77.                 R.drawable.foto9, R.drawable.foto10,
    78.                 R.drawable.foto11, R.drawable.foto12,
    79.                 R.drawable.foto13, R.drawable.foto14,
    80.                 R.drawable.foto15, R.drawable.foto16,
    81.                 R.drawable.foto17, R.drawable.foto18,
    82.                 R.drawable.foto19, R.drawable.foto20,
    83.                 R.drawable.foto21, R.drawable.foto22
    84.         };
    85.     }
    87. }
    And here is the main.xml file if needed...

    Code (Text):
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <GridView xmlns:android=""
    4.     android:id="@+id/gridview"
    5.     android:layout_width="fill_parent"
    6.     android:layout_height="fill_parent"
    7.     android:columnWidth="90dp"
    8.     android:numColumns="auto_fit"
    9.     android:verticalSpacing="10dp"
    10.     android:horizontalSpacing="10dp"
    11.     android:stretchMode="columnWidth"
    12.     android:gravity="center"
    13. />

  2. LeffelMania

    LeffelMania Active Member

    Jul 13, 2010
    Likes Received:
    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).
    rickynl likes this.
  3. rickynl

    rickynl Member This Topic's Starter

    Aug 23, 2010
    Likes Received:
    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!
  4. xkoldfuzionx

    xkoldfuzionx New Member

    Aug 31, 2010
    Likes Received:
    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.
  5. DavidDriver

    DavidDriver New Member

    Aug 31, 2010
    Likes Received:
    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!


Share This Page