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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:45:08+00:00 2026-06-18T14:45:08+00:00

I create an activity with dynamic buttons in a loop. I get a list

  • 0

I create an activity with dynamic buttons in a loop. I get a list and create a button for each element in the list. The buttons go to the same activity afterward, but with each button I want to pass different string.

I did this in the loop:

    tour_button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(TourListActivity.this,
                    TourMenuActivity.class);
            String info = tour.toString();
            intent.putExtra(TOUR_INFO, info);
            startActivity(intent);
        }
    }); 

But at the end, all the buttons get the same string (the string of the last button).

========================================
full code:

   try {
        JsonObject respObject = jsonParser.parse(response).getAsJsonObject();
        JsonArray tourListArray = respObject.getAsJsonArray("tours");
        System.out.println("tourListArray: " + tourListArray.toString());

        for(int i = 0; i < tourListArray.size(); i++){
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
            tour = tourListArray.get(i).getAsJsonObject();
            String tourCode = tour.get("tourcode").getAsString();
            Button tour_button = new Button(this);  
            tour_button.setText("Tour Code: " + tourCode);
            tour_button.setGravity(Gravity.LEFT);
            tour_button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Intent intent = new Intent(TourListActivity.this,
                            TourMenuActivity.class);
                    String info = tour.toString();
                    intent.putExtra(TOUR_INFO, info);
                    startActivity(intent);
                }
            }); 


            ll.addView(tour_button);

            LinearLayout yourLL = (LinearLayout) findViewById(R.id.Tours_List);
            yourLL.setOrientation(LinearLayout.VERTICAL);
            yourLL.addView(ll);  


        }
    } catch (JsonIOException e) {
        e.printStackTrace();
    } 
  • 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-18T14:45:10+00:00Added an answer on June 18, 2026 at 2:45 pm

    When you create the button you can:

     button.setTag(someString); 
    

    and then in the onClick you can:

    public void onClick(View v) {
            Intent intent = new Intent(TourListActivity.this,
                    TourMenuActivity.class);
            String info = tour.toString();
            intent.putExtra(TOUR_INFO, ((Button)v).getTag());
            startActivity(intent);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a dynamic button and have to access it in action
i create dynamic textviews in an activity with some fields returned by a webservice.
Like in topic - I want to create activity above tabHost that I already
I want to create an Activity which shows a sort of menu a user
I am trying to create a searchable activity however when I get to this
For each list view with difference row layout template, I must create each custom
I want to create layout with TabHost and I want to create activity for
I want to create an activity feed from recent article and comments in my
I would like to create an activity for my ClearCase snapshot view, but you
Hi I wish to create dynamic file view gallery images but I have no

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.