I am working my way through some of the android developers tutorial, specifically the Gallery View Widget located here I have made it through stages 1 and 2 yet I seem to be getting 3 errors and I cannot fathom what it is I have done wrong, as I have copied and pasted the code direct from the tutorial and made one change to the code, that being R.layout.events instead of R.layout.main
Here is the code
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.Toast;
public class Events extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.events);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter (this));
gallery.setOnClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(Events.this, "" + position, Toast.LENGTH_LONG).show();
}
});
}}
The errors that I am getting are as follows:
ImageAdapter cannot be resolved to a type line 18 Java Problem OnItemClickListener cannot be resolved to a type line 20 Java Problem
The method setOnClickListener(View.OnClickListener) in the type AdapterView is not applicable for the arguments (new OnItemClickListener(){}) line 20 Java Problem
Any advice or pointers would be greatly appreciated. Thanks in Advance.
-
We can't actually see what errors you're getting. Seems like it would be easier to help if we could.Dave Newton– Dave Newton2012年01月24日 20:21:01 +00:00Commented Jan 24, 2012 at 20:21
-
Sorry, I realized this post, posting and was adding them in whilst you was commenting.Qu1nncunxIV– Qu1nncunxIV2012年01月24日 20:23:56 +00:00Commented Jan 24, 2012 at 20:23
2 Answers 2
The ImageAdapter is a custom BaseAdapter defined further in the post, at point 6.
A custom BaseAdapter called ImageAdapter is instantiated (...)
And at point 6:
public class ImageAdapter extends BaseAdapter { //(...)
1 Comment
Wrong listener type. It should be View.OnClickListener.
gallery.setOnClickListener(new View.OnClickListener() { ....
Or use setOnItemClickListener...
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() { ...