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

  • Home
  • SEARCH
  • 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 9207623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:25:43+00:00 2026-06-18T00:25:43+00:00

I have an Activity with a ListView which I’ve added a listener ( setOnItemClickListener

  • 0

I have an Activity with a ListView which I’ve added a listener (setOnItemClickListener).
When I’m inside this method from the listener above mentioned:

    public void onItemClick(AdapterView<?> parent, View view, int pos, long id )

In order to have access to the Activity, I’ve done this:

    MyActivity host = (MyActivity) parent.getContext();

From host variable, I have access to all MyActivity methods and attributes (which aren’t static), but I can’t access to the MyActivity.this reference.

Could someone explain me why? Thanks

  • 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-18T00:25:44+00:00Added an answer on June 18, 2026 at 12:25 am

    MyActivity.this would reference the instance of the “outer” MyActivity class when your code ran in an inner class. — Does it? If so, why would you need the host variable?

    Update: It only just occured to me that you may be missing the fact that everything you would access via MyActivity.this from within an inner class, you can access via host in your specific setup.

    Update: You seem to have a wrong understanding of the meaning of this. Originally, it stands for this object, i.e. the class in which your code runs.

    With the additional notation Class.this, Java enables you to access the instance of the outer class of your code, but only from an inner class.

    In the following example, Class.this works:

    class A {
        int a;
        class B {
            void process_a_in_A() {
                A.this.a++;
            }
        }
        // however, this does NOT work b/c
        // static inner classes do not have references t oouter classes
        static class C {
            void process_a_in_A() {
                // error -- class A.C is detached from its outer class A
                A.this.a++;
            }
        }
    }
    

    Also, in the following example, Class.this does not work. The reason is simply that the system cannot know whether there is an Instance of class A and which one of the possibly many instances of class A you want to refer to.

    class A {
        int a;
    }
    
    class B {
        void process_a_in_A() {
            // A here is a class name, not an object reference;
            // so A.this is meaningless in this context
            A.this.a++;
        }
    }
    

    Once again, please be aware that you asked a question without providing the necessary information to answer it specifically, so it can only be answered in general and it looks like you have a wrong understanding about inner classes and the Class.this notation.

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

Sidebar

Related Questions

I have Activity with ListView inside it and in the onCreate method of the
I have an activity which have ListView. When I want to access this ListView
I have an activity which consists of a ListView . The problem is that
I have a ListView in my activity. Each row includes three TextViews in this
I have one SubjectTabActivity and i need to show listview on this activity. But
I have an activity with a ListView which is populated through a custom ArrayAdapter.
I have a listView which loads data from SQLite database and right now I
I have a listview which displays items retrieved from the webpage. Each item in
I have a button in listview which is getting populated from the arrayadpater class.I
Hi guys I have an activity which extends ListActivity and displays a listview which

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.