Coas Mckey Coas Mckey - 4 months ago 13
Android Question

How to specify which view is clicked in layout added multiple times

I have a layout which takes user's children information. Let's say that this layout takes info about the children's first name, last name, and age. I am taking inputs with EditText. And there is a spinner which shows children Allergy type, on selecting 1 Allergy type it has to fetch its details from webservice and to fill those details in a textview.

So in this way the User can add as many children as he wants. The problem occurs there. Let's say the user has added that layout 4 times now, he selects the spinner of children 1 and service gets called and it fetches the information and fills it in the last layout textview that was added.

where as it is expected the details should fill in each textview of each layout added accordingly.

Confusions :

  1. How can I exactly get which view is clicked and then how to take action in the same layout of that view not the one which is added recently?

  2. I am inflating layout which has the specific set of fields specified above. So I am maintaining the Array List of each layout added , so Is there a work around to get exactly the same view and its corresponding view in that layout ??

UPDATE 1: Some Idea of my question

There is a main activity it has 1 button named "Add More Children". When user click on this the layout which contains the children info adds in the specified area in the ScrollView, so that user can add as many children as much he want.

so basically I have drawn some views below where as there are too many views in the following layout named children layout.But this layout shows what type of work is need to be done . so see below and have some idea

Children layout

For demonstration you can see there are different edit text and spinners. Spinners get updated from Webs service and each children may have different data loaded in spinner from webservice. this whole layout inflates into the mainactivity. on button click named Add Children . so on that button I am adding this in the scrollview

private View addChildLayout(int childLayoutid, LinearLayout Targetlayout) {
//where childLayoutid is a layout resourse id of childern layout
// where Target layout is a scrollview in my fragment
LayoutInflater inflater = LayoutInflater.from(getActivity());
View inflatedLayout1 = inflater.inflate(childLayoutid, null, false);
inflatedLayout1.setId(numOfChildAdded);// numOfChildAdded is int number of children added by user so far
Targetlayout.addView(inflatedLayout1, 0);
numOfAddedChildLayout.add(inflatedLayout1);//this is an array list i am creating to keep track of each layout added by user on add button click

return inflatedLayout1;

after adding this in layout another method I call that finds the ids of this layout and sets the click listener of each views i.e spinners or edit text or whatever is needed. Now suppose User has added one children. and clicks on the spinnerAllergy types that fetches sub category and populate spinnerSubCatogry, and spinnerSubcategory when clicked the web service again gets called and gets details of allergy and fills in the Textview (the large box shown in picture)

Now the main problems comes in when user added Children no 2. now let suppose user has added 2 childs , but he clicks on the spinnerAllergy of Child1 , the child1 spinners runs the web service but populates the spinnerSubcategory of Child2.

It Looks like that when the new child is added the click listener refers to new layout which is newly added .

so that is the main problem . I want each view in each layout work accordingly in its views and boundaries. I mean child1 layout views click listener should populate its views not the currently added layout views.

I think that is enough information to get some clear idea.

Please help me with this, I've been stuck here for 2 days. Where as all my design is working good.


Well as you stated above that you are keeping track (List of ) every single view you have added. I will suggest you to use that

Here are the lines you are using and setting the id so its mean each parent has the different id where as their child views has the same id ,

 View inflatedLayout1 = inflater.inflate(childLayoutid, null, false);

As you are adding each layout with different ID why dont you simple get the parent and then again get the child with the specific id , for demonstration

Suppose there is a textview in you layout , and that under the Linear layout where as that linear layout has a relative layout as a root/main layout , and every main layout has different id as you have done above. so this is how you will go to the top(parent ID)

  ViewGroup row = (ViewGroup) yourTextView.getParent().getParent;
  TextView textView = (TextView) row.findViewById(; // the next view you can find 

so here I will suggest to do this with each view and then set click listener. this may not be more efficient but this would work. I have done that once When I was inflating my custom layout but keep that in mind every time inflating though may be quick but it would be costly , you need to implement some logic near to getview (as we have its implementation in adapter)

Why I am doing getParent().getParent() twice As I said that my textview is in linearlayout and that linear layout lies in the main/root layout and hence as we want to approach the mainlayout because we know it is the only one which has different Id , so we are doing getParent() twice as Textview has LinearLayout as first Parent and then the root layout comes, so in this way if you have a view in another layout , you need to dig it by yourself.

Again I am saying , it may be not a cool or best implementation , but it works . At least it worked for me.