Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:31:26+00:00 2026-06-10T01:31:26+00:00

I have an array on my Activity which consist R.drawables images declared inside a

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:31:27+00:00Added an answer on June 10, 2026 at 1:31 am

    You will have two options, either the Object Oriented way, and create a getter with a public/protected/default access modifier or else, just declare the array public/protected/default instead 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:

    public ActivityClass
    {
        private Integer[] imageIDs = ...;
    
        protected Integer[] getImageIDs()
        {
            return this.imageIDs;
        } 
    }
    

    And then in your extending class:

    public SomeOtherClass extends ActivityClass
    {
        ...    
        Integer[] imageIDs = this.getImageIDs();
        ...
    }
    

    After seeing this:

    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 );
                    //}
            }
        });  
    

    The problem is that this piece of code:

    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 );
                        //}
                }
            }); 
    

    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:

    GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
        final Integer[] imageIDs = this.getImageIDs();
    
        gridView.setOnItemClickListener(new OnItemClickListener() 
        {
            public void onItemClick(AdapterView<?> parent, 
            View v, int position, long id) 
            {   
                for (Integer imageId : imageIDs){
                    System.out.println(imageId );
                }
            }
        });
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listview with a custom array adapter: public class SmsMessageAdapter extends ArrayAdapter<ContactMessage>
I’m new to android,I have an activity class which fetches Json string from url
Problem 1. I have this class: public class ContactGroups { // Form an array
I have an activity which has an array list ArrayList<String> array = new ArrayList<String>();
As you can see, I have this PlayLesson_01 activity which displays images and audio
I have an Activity which lets the user input names and the save them.
I have a multiplechoice dialog on my activity. I have a boolean array to
I have an activity with a ListView which is populated through a custom ArrayAdapter.
I have a byte array which contains an image fetched from the net. I
I have method which return the latitude of the location public double[] getlat(){ double

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.