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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:55:05+00:00 2026-05-28T17:55:05+00:00

I have created a class of type BaseAdapter that is populated with buttons –

  • 0

I have created a class of type BaseAdapter that is populated with buttons – when you click on a button I want to load a new intent. This has proven difficult on two levels:

  1. You cannot associate the event with the button (one that creates a new intent) inside the Adapter. This is why I send the Buttons as an array to my Adapter (this solution works, but it is messy)

  2. Even though my buttons are created inside the same Activity – they cannot create a new intent from that Activity. The exeption is so great that I have not even gotten a try…catch statement to work.

I have tried reflection, creating the buttons inside the activity and passing them through, passing the context (to call context.startIntent(…))

My question: can someone show me how to create a ButtonAdapter where each button creates a new Intent – even of the same type as the original Activity?

UPDATE: Here is the code because I am getting answers from people who think I am struggling with onClickListeners:

public class ButtonAdapter extends BaseAdapter
{
    private Context _context;
    private Button[] _button;

    public ButtonAdapter(Context c, Button[] buttons)
    {
        _context = c;  
        _button = buttons;
    }

    // Total number of things contained within the adapter  
    public int getCount()
    {  
        return _button.length;  
    }  

    // Require for structure, not really used in my code.  
    public Object getItem(int position)
    {  
        return _button[position];  
    }  

    // Require for structure, not really used in my code. Can  
    // be used to get the id of an item in the adapter for  
    // manual control.  
    public long getItemId(int position)
    {  
        return position;  
    }  

    public View getView(int position, View convertView, ViewGroup parent) 
    {          
        _button[position].setId(position);  
        return _button[position];  
    }
}

—————
The Activity:

public class MainActivity extends Activity
{
    private GridView _gv;
    private TextView _heading;       
    private ButtonAdapter _adapter;

    public void LoadActivity(String heading)
    {
        try
        {
            Itent intent = new Intent(getApplicationContext(), MainActivity.class);
            intent.putExtra("Level", "NextPage");
            intent.putExtra("Heading", heading);
            startActivity(intent);
        }
        catch (Exception ex)
        {
            Toast.makeText(getApplicationContext(), String.format("Error LoadActivity: %s", ex.getMessage()), Toast.LENGTH_SHORT).show();
        }
    }    

    private void createButtonsAdapter(Button _button[])
    {
        _buttonadapter = new ButtonAdapter(getApplicationContext(), _button);
        _gv.setAdapter(_adapter);  
    }         

    private void setupButtons()
    {
        Button[] _buttons = new Button[2];
        String names[] = new String[]{"Button1","Button2"};

        for (int i = 0; i < 2; i++)
        {
            _buttons[i] = new Button(this);
            _buttons[i].setText(names[i]);
            _buttons[i].setTag(names[i]);

           _buttons[i].setOnClickListener(new View.OnClickListener()
           {
               public void onClick(View arg0)
               {
                   try
                   {
                       LoadActivity(((Button)arg0).getTag().toString());
                   }
                   catch(Exception ex)
                   {
                       Toast.makeText(getApplicationContext(), String.format("Error button.onClick: %s", ex.getMessage()), Toast.LENGTH_SHORT).show();
                   }
               }
           });
        }
        createButtonsAdapter(_buttons);     
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        _gv = (GridView)findViewById(R.id.gridview);
        _heading = (TextView)findViewById(R.id.tv_heading);                      

        Bundle params = getIntent().getExtras();                

        if (params == null)
        {
            setupButtons();    
        }  
        else if (params.containsKey("Level"))
        {
            _heading.setText(params.getString("Heading"));
            if (params.getString("Level").equals("NextPage"))
            {
                //code not here yet
            }
            else if (params.getString("Level").equals("Chapters"))
            {
                //future code
            }
        }
    }
}

Excuse the bold and caps but I have had enough silly answers to warrent this:
I HAVE TRIED PUTTING THE ONCLICKLISTENER INSIDE THE GRIDVIEW AND IT DOES NOT WORK EITHER
You cannout load an activity from a class outside that activity, even if you pass the context as a parameter. That is why I have resorted to this method, which completely bombs android, even though I have try catch statements.

Please try give me a solution in the form of a correction to my code, other code, or a tutorial that achieves what I want here. I know how to do a button adapter properly, it is the act of loading an Intent that has forced me to implement it this way.

  • 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-28T17:55:06+00:00Added an answer on May 28, 2026 at 5:55 pm

    Once again I am answering my own question:
    http://bottlecapnet.blogspot.com/2012/01/android-buttons-have-no-place-inside.html

    I will just have to style TextViews as I want to see them.

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

Sidebar

Related Questions

I have created a new class of type NSObject, which created two files --
I have created a class called Class1, and in another module, I want to
I have created a class that will manage connection and commands, I named it
I have created a type like this: TypeBuilder tb = moduleBuilder.DefineType(myname, TypeAttributes.Class | TypeAttributes.Public,
I have create a class that extends BaseAdapter . At the same time, tt
I have created a class so that I can bind it to a combo
I have created a class Car that derives form the abstract class TransportMeans and
I have created custom result class to serialize json data to xml. I want
I have created a Static Class and used that in Reflection. But when i
I have created a class Person that looks like this: public class Person {

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.