I have an array on my Activity which consist R.drawables images declared inside a
public class ImageAdapter extends BaseAdapter.
So it consists of
private Integer[] imageIDs = {
R.drawable.medal_my_first_perxtamp_gray,
R.drawable.medal_you_complete_me_gray,
R.drawable.medal_the_gift_of_sharing_gray,
R.drawable.medal_gimme_5_gray,
R.drawable.medal_a_cup_of_coffee_gray,
R.drawable.medal_seven_up_gray,
R.drawable.medal__gray,
R.drawable.medal_midnight_madness_gray,
R.drawable.medal_loyalista_gray,
R.drawable.medal_15_30_gray,
R.drawable.medal_kmh_gray,
R.drawable.medal_champion_gray
};
Now on my class that extends my activity how can I call that array and get a specific value? Sorry for a simple question but I am new to this Android Development
EDIT:
I forgot to add this one, a declaration to get the length of that array which is also stored inside ImageAdapter class
public int getCount() {
return imageIDs.length;
}
And added this code for getter
public Integer[] getImageIDs()
{
return this.imageIDs;
}
Sir
This is the code i am trying to create on my other class that Extends a activity
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)
{
//Integer[] imageIDs = this.getImageIDs();
// clicked picture here
Integer[] imageIDs = this.getImageIDs();
//Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",Toast.LENGTH_SHORT).show();
//Integer[] imageIDs = ImageAdapter.this.getImageIDs();
//for (Integer imageId : ImageAdapter.getImageIDs()){
// System.out.println(imageId );
//}
}
});
See that I can get the position but I dont want to use those tutorial using if else
Tutorial Here because I think it will be a long if else. What I am thinking is that since I have a array why cant I use that to get what I am looking for
You will have two options, either the Object Oriented way, and create a getter with a
public/protected/defaultaccess modifier or else, just declare the arraypublic/protected/defaultinstead of private.This should allow you to access the array in your extending class. If you just want to be able to access the array in the class which extends your class, just declare the array or getter method as
protected.So, you could do something like so:
And then in your extending class:
After seeing this:
The problem is that this piece of code:
Is a class within itself (known as an Inner Class). As far as the compiler is concerned, you are no longer within the class which extends from your activity. That would explain why the compiler complained like so:
The method getImageIDs() is undefined for the type new AdapterView.OnItemClickListener(){}.To go around this, all you need to do would be something like so:
The code above should work. It is important that you get your ID’s from outside the event handler, and that you declare the array as final so that it can be passed on to the inner class which is taking care of the event handling.