Hello, GridView Tutorial not working
I have followed the tutorial and can't seem to get it to work. Here's my code:
GridView.java:
package com.example.gridview;
import android.app.Activity;
import android.os.Bundle;
public class GridView 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.java
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
};
main.xml:
<?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"
All I get are error message everywhere. What did I do wrong? I followed everything the tutorial said to do.
The problem is, just like Matt said, the name of your activity is "GridView", and you're calling Toast.makeText(HelloGridView.this,... --> "GridView" is different that "HelloGridView", and they have to be the same name.
Ctrl+Shift+O helps too, because that will add all the necessary imports for the code.
This example in the android documentation does not include appropriate namespaces still as of december 2011 when I just looked at it. So, as mentioned above clicking ctrl -shift - o in your activity class will import all of the appropriate namespaces.
However, in addition to this then on this line Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
you need to make sure that HelloGridView is the name of the actual Activity class that you are working in. For instance, the name of my class was actually HelloGridViewActivity and so this line was erroring out for me.
And finally, when you create the imageadapter.java class just make sure that this class is in the same namespace as your activity java class, ie testing.examples.HelloGridView for instance.
These 3 items allowed me to run the sample.
I was having a wee bit of a problem with this too.
To solve all the errors I just pressed ctrl-shift-o in eclipse to import everything. Then in my ImageAdapter.java I made sure I defined the package at the top. So make sure package com.example.gridview; is in your ImageAdapter.java
Hope that helps.
So I ran into the same issue, and after hitting Ctrl+Shift+O on each page, then after I changed the HelloGridView to HelloGridViewActivity at the very bottom on the HelloGridViewActivity.java class, I was only left with the drawable folder issues.
In the tutorial it uses a drawable folder. You only get a drawable-hdpi, drawable-mdpi, and drawable-ldpi, so you have to create a new folder in your res folder called drawable in order for the code to work. If not, you can always put it in the drawable-hdpi folder and do a find/replace.
Good luck!
Can you just go to project and select "Android Tool->Project Properties" then clean up the project "Project ->Clean". If it still occurs, go to the AndroidManifest.xml and check if there are any errors
In ImageAdapter
class getItem
and getItemId
return must be position. You can put position there and try again.
http://www.mkyong.com/android/android-gridview-example/
You have not import all necessary classes and Eclipse sometimes don't use to import classes by clicking.
Press (Ctrl+Shift+o) to import all necessary classes where i fixed it
MainActivity.java
// import all packages:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(getApplicationContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick1(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
}
this will help. keep Coding..!!
精彩评论