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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:56:46+00:00 2026-05-25T10:56:46+00:00

I have a super TestUI application. It has a gridview with buttons in them.

  • 0

I have a super TestUI application. It has a gridview with buttons in them.

I want word of the button click to be passed back to the main Activity, so that it can update its state.

Sadly the buttons steal the clicks. So the typical:

        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Toast.makeText(TestUI.this, "" + position, Toast.LENGTH_SHORT).show();
            }
        });

Does not get called.

Instead in the adapter definition:

   public View getView(int position, View convertView, ViewGroup parent) {

    Button gridItem;

    if (convertView == null) {  // if it's not recycled, initialize some attributes
        gridItem = new Button(mContext);
    } else {
        gridItem = (Button) convertView;
    }
    gridItem.setText("button " + String.valueOf(position));
    gridItem.setClickable(true);
    gridItem.setFocusable(false);
    gridItem.setOnClickListener(new MyOnClickListener(position, mContext));

    return gridItem;

Which is backed with a class MyOnClickListener that implements the OnClickListener interface. However if I do it this way I still need a call back to the main activity, somehow it needs to know that something was done as it controls the program state.

So what is the best way to update the “root” class/activities’ state from a button click?

I know this a basic OO question but I mostly write in ASM and C so I frankly just don’t know.

  • 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-05-25T10:56:46+00:00Added an answer on May 25, 2026 at 10:56 am

    As you said, your getView method is inside the adapter class. But the adapter gets a copy of the “context”, which your getView method has access to. Context is a super-class of all Activities and Services. Through this object you can do many of the basic operations of an Activity. From your example, I understand that you would like to show a toast. This can be done using

    public View getView(int position, View convertView, ViewGroup parent) {
        //usual getView stuff goes first
    
        gridItem.setOnClickListener(new OnClickListener(){
            public onClick(View v){
                Toast.makeText(mContext, "" + position, Toast.LENGTH_SHORT).show();
            }
        }
    }
    

    So in short, your MyOnClickListener class needs to used the mContext variable for interacting with the root activity.

    Edit
    I realized that I made a mistake in the above answer. I looked at the sample on http://developer.android.com/resources/tutorials/views/hello-gridview.html and realized that you aren’t restricted to using Context. You could use something like

    public class MyActivity extends Activity {
        public int a = 0;
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            GridView gridview = (GridView) findViewById(R.id.gridview);
            gridview.setAdapter(new ImageAdapter(this));
        }
    }
    
    public class ImageAdapter extends BaseAdapter {
        private MyActivity myAct;
        public ImageAdapter(MyActivity c) {
            myAct= c;
        }
        public View getView(int position, View convertView, ViewGroup parent) {
            //usual getView stuff goes first
    
            gridItem.setOnClickListener(new OnClickListener(){
                public onClick(View v){
                    myAct.a = position;
                }
            }
        }
    }
    

    Hopefully that is what you wanted to do.

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

Sidebar

Related Questions

I have a super-trivial application on GAE that I want to upgrade to HRD
I want to uninstall some useless apps from /system . I have super access,
Noob here. I have a super column family sorted by timeuuidtype which has a
I have a 'super-repository' that has a submodule that is essentially a fork of
I have a super strange problem. I want a Textured window to have it
I have a super class and two subclasses and I want to serialize the
For example, I have a super simple widget that I want to allow my
I have a super pom defined in which I specify a "resources" directory for
I have a super-simple query in a star schema. One fact, two dimensions. I
I have a super fish menu, the delay is set to 800, but when

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.