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 7839755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:32:27+00:00 2026-06-02T15:32:27+00:00

I have an OnItemClickListener implementation defined that I use in several Activities and Fragments

  • 0

I have an OnItemClickListener implementation defined that I use in several Activities and Fragments throughout my application. I’m currently passing the Activity in the constructor, so I can call startActivity without getting an error. I wasn’t sure if this is the appropriate approach.

Is there anything I am doing wrong?

public class EventSelectedListener implements AdapterView.OnItemClickListener {

    private Activity mActivity;
    private Context mContext;

    public EventSelectedListener(Activity activity) {
        mActivity = activity;
        mContext = mActivity.getApplicationContext();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
         Intent intent = new Intent();             
         intent.setClass(mContext, MediaPlayerActivity.class);
         mActivity.startActivity(intent);       
    }
}
  • 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-02T15:32:30+00:00Added an answer on June 2, 2026 at 3:32 pm

    Application’s context and Activity’s context are not the same thing. In your code you are supposed to use Activity’s context.

    Change this:

    mContext = mActivity.getApplicationContext();
    

    To this:

    mContext = mActivity;
    

    Or

    Alternatively, you dont even need to bother about keeping a separate context object since your mActivity is already having reference to context. So, simply use mActivity where ever you need to refer to context:

    public class EventSelectedListener implements AdapterView.OnItemClickListener {
    
        private Activity mActivity;
    
        public EventSelectedListener(Activity activity) {
            mActivity = activity;
        }
    
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
             Intent intent = new Intent();             
             intent.setClass(mActivity, MediaPlayerActivity.class);
             mActivity.startActivity(intent);       
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Activity, I have two Gallery items. I am implementing OnItemClickListener so I
I have this in my first activity: private AdapterView.OnItemClickListener _itemClickLis = new OnItemClickListener() {
I currently have an Android ListView class that displays a list of about 20
I have read about problems that onClickListener will consume onItemClickListener Events like here or
I have an error that says OnItemClickListener cannot be resolved to a type when
In our Android app, I have an activity that has a gridview that displays
I have a class that extens a listview. I need to have onitemclicklistener on
I have set onItemClicklistener on my custom list view but onItemClicklistener not working I
I have 3 errors in the code of notification using OnItemClickListener i need to
When we implement OnItemClickListener , we have to implement onItemClick method which is an

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.