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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:45:06+00:00 2026-06-15T02:45:06+00:00

I want to create by code an array of objects that are subclasses of

  • 0

I want to create by code an array of objects that are subclasses of Button.

public class MyButton extends Button {
    private Context ctx;
    private int status;
    public MyButton(Context context) {
        super(context);
        ctx = context;
        status = 0;
    }

    private click() {
        status = 1;
        // OTHER CODE THAT NEEDS TO STAY HERE
    }
}

In the main activity I do this:

public class myActivity extends Activity {
    private MyButton[] myButtons = new MyButton[100];

    @Override
    public onCreate(Bundle si) {
         super.onCreate(si);
         createButtons();
    }

    private void createButtons() {
         for (int w=0; w<100; w++) {
             myButtons[w] = new MyButton(myActivity.this);
             myButtons[w].setOnClickListener(new View.onClickListener() {
                  public void onClick(View v) {
                  // ... (A)
                  }
             });
         }
    }
}

Now I want the click() method inside MyButton to be run each time the button is clicked.
Seems obvious but it is not at my eyes.

If I make the click() method public and run it directly from (A), I get an error because myButtons[w].click() is not static and cannot be run from there.

In the meantime, I an not able to understand where to put the code in the MyButton class to intercept a click and run click() from there. Should I override onClick? Or should I override onClickListener? Or what else should I do?

How can I run click() whenever one of myButtons[] object is clicked?

Thanks for the help.

  • 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-15T02:45:09+00:00Added an answer on June 15, 2026 at 2:45 am

    you can add:

    View.onClickListener onclick = new View.onClickListener() {
                      public void onClick(View v) {
                         ((MyButton)v).click();
                         //since v should be instance of MyButton
                      }
                 };
    

    to your Activity
    then use:

    myButtons[w].setOnClickListener(onclick); 
    //one instance of onclick is enough, there is no need to create it for every button
    

    in createButtons()

    but … why, oh why array of buttons we have ListView in android …

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

Sidebar

Related Questions

i want create Dynamic Slideshow in Jquery i'm Write this code var ctx =
I want to create 1MB String for benchmark,so I writed code as follow: public
I want to create a helper method that could be called from different code-behind
I want to create an object to store jQuery objects. Here is my code
I want to create a QMenu that contains QAction objects that are checkable. Once
I want to create a web method that accepts a List of custom objects
I want to create tabs which will show certain views I create through code.
I want to create an item to sitecore using code behind. I found this
I want to create a capped collection from Java code. I found the syntax
i want to create a validatior via code behind.But although i create it,it doesnt

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.