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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:46:27+00:00 2026-05-27T22:46:27+00:00

I want to create n number of EditText objects …so I am using LayoutInflater

  • 0

I want to create n number of EditText objects …so I am using LayoutInflater to do that in a loop which runs till n …The views are added to the layout but if I want to add a addTextChangedListener() to each of the EditText objects , the listener is added to only the last object …its a problem of closure ,how can I solve this ?

    LinearLayout ll=(LinearLayout) findViewById(R.id.i1);
    LayoutInflater li=(LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    for(int i=0;i<3;i++){
        v=li.inflate(R.layout.addit, null); //v is a View declared as private member of the class
        t=(TextView) v.findViewById(R.id.a1);//t is TextView declared as private member 
        EditText e=(EditText) v.findViewById(R.id.e1);
        e.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {

            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) {

            }

            public void afterTextChanged(Editable arg0) {
                ret(v, t);
            }
        });
        ll.addView(v);

    }

ret function

   public void ret(View v,TextView t){
    EditText e=(EditText) v.findViewById(R.id.e1);
    t.setText(e.getText());

}

main xml :

   <?xml version="1.0" encoding="utf-8"?>

   <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"  
     android:id="@+id/i1"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical" >

  </LinearLayout>

addit.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/l2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
    android:id="@+id/a1"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:text="" />
<EditText
    android:id="@+id/e1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName" >

    <requestFocus />
</EditText>

</LinearLayout>

The listener only listens for changes in the last of the EditText views …how to make it to listen to all three EditText views?

  • 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-27T22:46:28+00:00Added an answer on May 27, 2026 at 10:46 pm

    This is because your vand tvariables is overwritten in each loop, making them point to the last View and TextView.

    Try changing your code to:

    final View v=li.inflate(R.layout.addit, null);
    final TextView t=(TextView) v.findViewById(R.id.a1);
    
    • 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 number of hidden objects via jQuery, that (once all
I want to create a report which aggregates the number of activities per customer
I want to create a number of links within a page that have different
I want to create a function which will a number and the position of
I want to create a class that could hold any of a number of
I want to create a function in C# which for a week number will
I want to do something like this: Create a number of assemblies that implement
I want to create an index page for which I need the page number
I want to create a number of div s that I can move and
I want to create a Gallery that displays a very large number of images.

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.