1

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.

Coas Mckey
  • 701
  • 1
  • 13
  • 39

3 Answers3

1

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);
inflatedLayout1.setId(numOfChildAdded);

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(R.id.tvClassLevels); // 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.

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54
0

I think the problem is : Different views in your layout have SAME id since you're inflating from the same xml layout.

You have different approaches to solve this:

  • Keep track of a variable and increment the count whenever you add a Child View. Once the inflation is done, you call setId on the newly created View(just as mentioned in Abdul Salam Ali's answer). Although view ids in android are generated at compile time(correct me if I'm wrong), this should work perfectly in practice.
  • Create a Ids resource file in res/values folder and pre-define a few Id values for future use. It's guaranteed that all id values will be distinct. But as you say, users can add as many child info as they want, this may not be the best choice.
  • Use Random to generate different ids.

Please note that even if you have same Ids in your layout, your code logic would be correct as long as the childs of ViewGroup on which you call findViewById(or sth similar to this) has a unique id. See this

Community
  • 1
  • 1
suitianshi
  • 3,300
  • 1
  • 17
  • 34
  • The link you have attached is great explanation . I think the answer above (Abdul Salam Ali's Answer) is most complete and indicates not only the real problem but also he told the solution , well at first glance i was unable what he was saying by getParent().getParent() . Actually he was indicating the problem that may occur in future , and that I have faced it when one of my checkbox was inside multiple layouts(nested) then it really worked for me . – Coas Mckey Sep 06 '16 at 03:58
-1

For some suggestion to your design.

  1. Use the listview and adapter design instead of generating the view in programming.

    a. Add the new item

    Used floating action button. https://material.google.com/components/buttons-floating-action-button.html#buttons-floating-action-button-floating-action-button

    B. Remove the item

    Used some list control or multi selector to remove the undesired items.https://material.google.com/components/lists-controls.html#lists-controls-types-of-list-controls

  2. Use listview to show your item instead of generating layout in your code.

    https://material.google.com/components/lists.html#lists-usage

  3. To edit the specific child information, suggest to use the full screen dialog for editing.

    https://material.google.com/components/dialogs.html#dialogs-full-screen-dialogs

The above is my suggestions toward your design, since it would be difficult if you want to get the clicked view's parent to find your actual position in the layout, and then send to to listen the corresponding on click event. It would be hard to maintain and your code will be hard to debug if there is any nested view in your parent. Try to separate each part to help your debug what is wrong in your code.

Long Ranger
  • 5,888
  • 8
  • 43
  • 72
  • for some reasons I can not use Listview , So can you tell How can I get the parent – Coas Mckey Sep 01 '16 at 10:07
  • If you need to find the view hierarchy based on the view you click, you can take a look on this(http://stackoverflow.com/questions/7484471/loop-through-all-childs-of-groupview , http://stackoverflow.com/questions/17879743/get-parents-view-from-a-layout). But you should know, it is not a good practice to do it. – Long Ranger Sep 02 '16 at 01:55
  • what should i do OR what else you would suggest me – Coas Mckey Sep 02 '16 at 03:26
  • I want to reconfirm, is your question about how to get the position based on the click view in the layout? Also, it you need some solid suggestion based on your requirement, please post some simple code of your work and we can take a look on it. – Long Ranger Sep 02 '16 at 03:33
  • though there is a lot of code that I need to publish in this concern but let me put some pieces of it – Coas Mckey Sep 02 '16 at 04:08
  • some codded idea has been added – Coas Mckey Sep 02 '16 at 04:47
  • Your answer does not point to real problem also it leads and opens another conversation which does not relate to the question – Coas Mckey Sep 02 '16 at 10:52
  • Ok fine, I removed the code. If you are going to find the specific view based on the spinnerAllergy spinner you clicked and populate to the corresponding spinnerSubcategory spinner, you should set the OnItemSelectedListener for the spinner and inside the function you should get the parent viewgroup of the spinner. And then use the method I provided in my old comments. After you located the parent , you can use the findViewById function to find your spinner and then set the spinner information. It is also true for your second spinner to call the web service. – Long Ranger Sep 02 '16 at 11:25
  • I have founded the way , thanks for your concern. and now you are thinking right, also look at the links you have provided for each think above , i.e https://material.google.com/components/lists.html#lists-usage do you really think this link is right for demonstration of listview ? – Coas Mckey Sep 02 '16 at 12:39