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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:11:00+00:00 2026-06-04T21:11:00+00:00

I tried to setup a onClickListner in a while loop like this: View inflaterLayout;

  • 0

I tried to setup a onClickListner in a while loop like this:

    View inflaterLayout;
    LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearLayout2);      
    while (counter < 5) {
        inflaterLayout = LayoutInflater.from(getBaseContext()).inflate(R.layout.newplayerlayout, null);
        myLayout.addView(inflaterLayout); 
        Button testButton = (Button) myLayout.findViewById(R.id.button2);
        testButton.setId(testButtonArray[counter]);
        ((TextView) myLayout.findViewById(R.id.textView1)).setId(testTextArray[counter]);
        testButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                ((TextView) findViewById(testTextArray[counter])).setText("Hi!");

            }
        });
        counter++;
    }

And what I thought it would do is when you click any one of the testButton’s it would change the TextView next to it! But only the first button thats inflated works! Does anyone know what I’m doing wrong?

Basically what I’m trying to accomplish is, I’m inflating a layout that has a Plus Button, Minus Button and a Text View, so I want to be able to setup one onClickListener that will add and subtract from the text view without having to setup a million separate onClickListeners!

  • 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-04T21:11:01+00:00Added an answer on June 4, 2026 at 9:11 pm
    new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((TextView) findViewById(testTextArray[counter])).setText("Hi!");
        }
    

    The code inside the onClick function is run when the button is clicked. It does not remember the values of the variables during each iteration of the loop, simply because it is in a different scope.

    Update

    The following code only uses one OnClickListener to increment or decrement the value of each corresponding TextView. You should be able to easily adapt this to your layout inflater with a better looking layout.

    public class Example extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            LinearLayout layout = new LinearLayout(this);
            layout.setOrientation(LinearLayout.VERTICAL);
    
            OnClickListener listener = new OnClickListener() {
                public void onClick(View view) {
                    TextView text = (TextView) view.getTag();
    
                    if(((Button) view).getText().equals("+"))
                        text.setText(Integer.parseInt(text.getText().toString()) + 1 + "");
                    else
                        text.setText(Integer.parseInt(text.getText().toString()) - 1 + "");
                }
            };
    
            for(int i = 0; i < 5; i++) {
                LinearLayout line = new LinearLayout(this);
                line.setOrientation(LinearLayout.HORIZONTAL);
    
                TextView text = new TextView(this);
                text.setText(i + "");
                line.addView(text);
    
                Button plus = new Button(this);
                plus.setTag(text);
                plus.setText("+");
                plus.setOnClickListener(listener);
                line.addView(plus);
    
                Button minus = new Button(this);
                minus.setTag(text);
                minus.setText("-");
                minus.setOnClickListener(listener);
                line.addView(minus);
    
                layout.addView(line);
            }
    
            setContentView(layout);
        }
    }   
    

    Addition from comment

    I am going to assume that in your newplayerlayout.xml you have two Buttons (with the ids: “plus” and “minus”) and a TextView (with the id “text”). Perhaps you would implement the scheme like this:

    OnClickListener mListener = new OnClickListener() {
        public void onClick(View view) {
            TextView text = (TextView) view.getTag();
    
            if(((Button) view).getText().equals("+"))
                text.setText(Integer.parseInt(text.getText().toString()) + 1 + "");
            else
                text.setText(Integer.parseInt(text.getText().toString()) - 1 + "");
        }
    };
    ...
    
    
    while (counter < 5) {
        inflaterLayout = LayoutInflater.from(getBaseContext()).inflate(R.layout.newplayerlayout, null);
        myLayout.addView(inflaterLayout);
    
        TextView inflatedText = (TextView) inflaterLayout.findViewById(R.id.text);
        Button testButton = (Button) inflaterLayout.findViewById(R.id.plus);
        testButton.setTag(inflatedText);
        testButton.setText("+");
        testButton.setOnClickListener(mListener);
    
        testButton = (Button) inflaterLayout.findViewById(R.id.minus);
        testButton.setTag(inflatedText);
        testButton.setText("-");
        testButton.setOnClickListener(mListener);
    
        counter++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While installing the new eclipse indigo IDE I've tried to setup the tomcat server
I've tried putting (setq term-setup-hook 'vip-mode) in my .emacs file but this only seems
I tried to setup role provider, using this article . It worked partially -
This is the first time I've tried to setup Joomla in a localhost dev
I checked this link of S.O : Pop Images like Google Images And tried
I am new to ColdFusion and ColdBox (and programming). I tried to setup ColdBox
Tried this: $('.link').click(function(e) { $.getScript('http://www.google.com/uds/api?file=uds.js&amp;v=1.0', function() { $('body').append('<p>GOOGLE API (UDS) is loaded</p>'); }); return
I've setup a footer to my list view, next I'm setting up an onclicklistener
Has anybody tried to setup sbt to work with Google App Engine? I dream
I have a delphi application with multiple forms. Initially I had tried a setup

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.