Dynamically TreeLayout - android

I've been trying to create this logic for 2 days and even though on paper looks like a relatively simple problem, there are a lot of cases that mess this up while effectively writing the code.
After querying a web service, I receive this type of response:
I need to create a layout dynamically depending on the answer to simulate this behavior:
There is the main spinner that has all the root items. If I select one item there is 2 choices. If it doesn't have any children, I only add a textview. Otherwise I add a new spinner that has the children as options and if a children is selected, a new textview is created underneath. This goes recursively.
I successfully implemented this and it works only for 2 levels (the json presented without the id '5' element). I want this to work for an unlimited number of levels. The problem appears when i have a 3 level tree and i select on the middle node. I want to hide his edit text, but also the spinner and the edit text of his child and I don't have a direct access to the newly programmatically created views.

Use recursive function. Stop recursion if child's node is empty. So you make first call on first item, if it have no children go next, if it have children call this function again.
Deleting view depends on root element you've selected. If it is RecyclerView then just update collection and notify adapter/use DiffUtil callback.
If you adding view dynamically then just set View id on creation and remove view by id on updates.

Set id to dynamically created view using View.generateViewId() like urview.setId(View.generateViewId()) and as per your need, remove them using id.

Related

Iterative through recyclerview's items and select one that matched the condition using Appium and Robot Framework

I'm working on automated testing using Appium and Robot Framework. I have an issue dealing with an item in Recyclerview. The recyclerview's item is a custom view which is a combination of mutiple textviews. Let's assume that the recyclerview's item looks like this
======================
TextViewA | TextViewB | TextViewC |
======================
Where
TextViewA's id is "tvA"
TextViewB's id is "tvB"
TextViewC's id is "tvC"
My goal is to write a robot to iterative through the items and click the Textview (any of three) if the item matched the following conditions:
#text in TextviewA must be equal ${paramA}
#text in TextviewB must be equal ${paramB}
#text in TextviewC must be equal ${paramC}
I decided to do the following:
Get all items in recyclerview as a list and loop through the items. How? - Impossible because the list is a RecyclerView
If 1. is impossible, create a while loop (for loop where ${i} = 0 - 9999) and check that is item[${i}] exists. If the item exists, check whether the #text in TextViews matched the parameters. If the item does not exists, scroll up and check the item again
I am not familiar with appium or the robot framework, but if this is a visual test then I'm sure you can mock the inputs, right?
What I would expect a test to cover is that given a list with items A1, A2, A3, that there are three rows in a RecyclerView whose values are bound to A1, A2, and A3's contents.
You can't iterate all items of a RV like that, because the recycler view well.. recycles views, so it doesn't create all Views. It just uses a pool that gets reused as you scroll.
About your option 2
create a while loop (for loop where ${i} = 0 - 9999) and check that is item[${i}] exists. If the item exists, check whether the #text in TextViews matched the parameters. If the item does not exists, scroll up and check the item again
I think you're approaching this from the wrong angle. First you ought to assume the recycler view "works as intended" by design. You don't need to ensure it does what it's supposed to do, but you do want to ensure that if you give it 5 items, those items are correctly bound to what you expect, and that the bindings are correct.
In Espresso/Android you'd normally instantiate your Activity/Fragment "scenario", and override the injected dependencies (namely, for e.g.: the data source where your items normally come from), to replace them with a simpler/fake/faster data set.
So instead of making a network request during your test you'd simply provide a known/hardcoded set of values.
E.g.:
If your activity/viewModel use a repository to fetch this list, you can inject a mock that returns a List that you hardcoded in the test and that you know.
So when the activity starts for real, it uses this fake repository.
At this point then you KNOW what the screen should look like. If your fake list had 10 items, then your RecyclerView should be capable of rendering these 10 items.
For that you'd use actions like:
onView(withId(R.id.your_recycler_view))
.perform(actionOnItemAtPosition<YourViewHolder>(0, click()))
For example, to "click" on an Item at position 0. Or you can use many of the other Espresso actions, assertions, matchers, to determine if certain text is present, etc.

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 :
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?
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
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);
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.
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
For some suggestion to your design.
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
Use listview to show your item instead of generating layout in your code.
https://material.google.com/components/lists.html#lists-usage
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.

define a recurrent set of views to use multiple time in the same layout

I'm working on an android project where I have a set of views (2 TextViews and some checkboxes in a checkbox group) to replicate many time in the same activity.
Is it possible to define the layout only for one set and instantiate it many time?
Also, the views are grouped in a Relative layout, is it possible to position the without the id attributes (to avoid id duplication)?
I would use a ListView for this. Even if you got like 5 items it workd fine. If you got many more items it still works perfect. Take a look at this example.
You can do this by defining the fields you want to reuse in their own xml. you can then use the 'include' tag for where you want them to display.
http://developer.android.com/training/improving-layouts/reusing-layouts.html
You do need to define the id's to position them in the relative layout. What is your concern about replicating the id's.
The other thing worth mentioning is how to use findById() when using 'include'. You can put a id on the include tag (which is effectively the relative layout viewgroup). Find that group first (Cast to viewgroup) and then do a findbyId on that group for what ever view you are after.

Which is the better method in Android for creating a dynamic list?

If you are creating a very dynamic list, say, where every row can have a different set of input types plus optional buttons, and the list length is based on another dynamic value, is it better to do this in a list adapter or creating a custom view in a scroll window?
After struggling with list adapters for quite a while now something finally occurred to me- this seems dumb. It seems like I am going through a lot of work keeping track of what spinner is set to what value, which row was clicked and so forth.
For example, say you are showing something like a contacts screen with various details that can be entered about a contact. Some rows will have text inputs (name, address etc), some will have spinners (ie. state, group), some will have checkboxes (like 'favorite' or something). Also, there is an 'add' button that allows you to add another field to edit. Is it worth making this in a list adapter or is it better to populate a custom view, and if the "add" button is clicked, we re-create the custom view, adding a view of the type they want to add?
I hope this is clear.
ListViews (and List Adapters) are meant for data that is to be displayed in mainly similar views. For your example, it is much easier and more natural to have a predefined layout file with the screen and use view visibility so select which views are to be shown. If you need to add views to the screen you can do this dynamically by using findViewById on the layout and then using it's addView method.
Let me know if you need more clarification or sample code...

How to get all children (visible and invisible) from a ListView?

My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.
I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).
I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:
getListView().smoothScrollToPosition();
But this doesn't work.
I think that I don't need to post the rest of my code, as the description says everything?
As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.
But the ListView doesn't keep the current values from RadioGroup in
running time.
I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.
I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.
The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.
If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.
The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.
There are a few point that you need to take care of:
1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things.
2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time.
3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.

Categories

Resources