1

My program asks a user to enter their name and click on a button called btn. Once btn is clicked, their name is dynamically added to a TableRow along with another dynamically created Button. It's these Buttons that I'm having an issue with. I need to somehow access them later on in the program. I created a number of IDs in my res/value folder to keep track of each one(changebtn1, changebtn2, etc..). They're all stored in an array called buttonIDs.

Let's say that the user enters the first name, a new row is created with a dynamically created button:

 btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tableRow = new TableRow(getApplicationContext());
            Button changeButton = new Button(getApplicationContext());

            changeButton.setText("Change");
            changeButton.setId(buttonIDs[i]);//From res/values

            tableRow.addView(changeButton);
            tableLayout.addView(tableRow);
            i++;
        });

Now let's say they enter a second name, another Button is created and so on and so forth. How can I now set an OnClickListener to my first Button that I created, which has the ID of R.id.changeBtn1? In other words, I have all of these dynamically created buttons and am not sure how to add OnClickLsteners() to earlier ones or access them in anyway. Thank you

3 Answers 3

1

Or you attach the OnClickListener directly in the creation of the button or you can store the references to the buttons like this:

ArrayList<Button> buttons = new ArrayList<Button>();
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        tableRow = new TableRow(getContext());
        Button changeButton = new Button(getContext());
        buttons.add(changeButton);

        changeButton.setText("Change");
        changeButton.setId(buttonIDs[i]);//From res/values

        tableRow.addView(changeButton);
        tableLayout.addView(tableRow);
        i++;
    });

for(Button button: buttons){
    button.setOnClickListener(new OnClickListener()
       ...etc...
    );
}

You won't waste a lot of memory since the buttons.add() line won't copy the button in the array but just the reference to the button. If you need a in id access to the buttons, use an HashMap, like this:

HashMap<String, Button> map = new HashMap<String, Button>();
map.put("id", new Button(getContext()));

And then access it like this:

Button button = map.get("id");
Sign up to request clarification or add additional context in comments.

Comments

0

How about something like this

for(int i=0;i<buttonIDs.size();i++) {
   Button currentButton = (Button) findViewById(buttonIDs[i]);
   currentButton.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
          // Stuff
       }
   }); 
}

I did not check the code

Comments

0

@filnik The first part of your answer also gave me an idea. I created an OnClickListener method outside of my OnCreate() method.

View.OnClickListener changeTeamName(final Button button)  {
    return new View.OnClickListener() {
        public void onClick(View v) {
            //Do Stuff
        }
    };
}

I then set an OnClickListener to EACH dynamically created Button and use the method that I created.

            changeButton.setText("Change");
            changeButton.setTag("ButtonOne");

            changeButton.setOnClickListener(changeTeamName(changeButton));

The fact that each Button now has an OnClickListener associated with it, they can now perform whatever function I add to my method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.