dynamic layout based on dynamic data in android - java

i need to get some json data from a server and then create elements to show that data. due the data is into an array of objects, i need to put this code into a loop.
example:
name - address
save button
name - address
save button
name - address
save button
i am thinking to create an array of linearlayouts with the needed widgets inside and repeat this process n times.
am i going right? it would be great if you can help me with a point of view.
kind regards and thanks for advance.

I would create a ViewGroup, then add each child view to that. The reason for this is it is more flexible (as the LinearLayout inherits from the ViewGroup). Then you can loop through each view later to get their values if need be

no, you should use a ListView and put your data in an ArrayAdapter. There are lots of examples and tutorials for that.

Related

Correct approach on using RecyclerView?

My task is to develop a list of different type of questions, a survey. It could include types like Integer-Answer-Question, Long-Text-Answer-Question, and so on.
Why is it needed to be a list? Because for the people using the app is way better to scrolldown answering each question rather than swiping to right, or doing another movement.
So I was face to face with the dilemma of using a ListView or a RecyclerView. My research gave the final outcome of using a recyclerView and having a viewHolder for the different types of questions that I have.
The struggle came when I realized that there is a type of question that has dependency related to it; how so? if you select one option then you have to "show" some questions, and if you deselected this option then you have to "hide" it again.
The thing is that I need to know the reference of each question to their viewHolder in order to "show" o "hide" each of them, but if the recycler is recycling viewHolders then it could create a mess on my logic.
My punctual questions are: Am I using the correct component with the RecyclerView?, is there any way to access a viewHolder with a unique reference, like and id or something?.
if you need me to show some code, I'd do it happily.
Valuable information:
if you are interest in how notifyDataSetChanged() works you can access to this link for further and detail information.
Minas mina's approach was the correct one!
If I understand correctly, you need to hide some types of questions when the user selects an option.
Your understanding of what the view holders are supposed to do is not quite right. The view holders cache a bunch of views that you later use in onBindViewHolder() to fill-in data from your model objects.
The actual model objects should be in your adapter. In your case, something like
List<Question> questions
In onBindViewHolder(), you fill-in the fields of the View holder with the data from a Question object.
As for your question, what you can do is to set a flag in your recyclerView adapter, e.g. hideQuestionsOfTypeA to true and then call notifyDatasetChanged() on the adapter.
When binding objects, check if that flag is true and if yes, set the visibility to GONE to the views that need to be hidden.

Android - Can you 'auto create' layouts while app is running?

I'm trying to create a scrollabe friends list. I'm grabbing the information for each friend off of a text file (profile pic, name, age, etc.)
So in my prototype I used different nested layouts to create one single friend entry. And to fill the list I just copied the most parent layout of my first friend and changed or adjusted the profile pic and the other text information.
What if, while the app is running, I need to update my friendslist and add another friend to the list.
How do you realise something like that?
Go for a recycler view or a scrollable list view to make a list and dynamic. You can check slidenerd's video tutorial on youtube. They are really helpful for a beginner to start any kind of app.

Get overall string text of different dynamic views

I have an android fragment that looks something like this:
Every time the user clicks add/remove a new row is added/removed. When they click okay, then I need to return a string for all the views above, for example "TextATextBTextCTextD".
What would be a good way to go about this? I thought about adding tags for each new view, then doing a for loop through each view. But because I have spinners and edit views, I wasn't sure how to get the view for each then get their text.
Any help would be appreciated. Thanks.
Well, your tag option isn't bad... everytime you change one of the view's values, you could set the tag of the view to the string value, then iterate over the views and just collect the tags.
Another way to do it, would be to create your own custom spinner / edittext & make them all implement an interface which has a method called String getDisplayText()
Then, whenever you create a view, place it in an array of that interface type, then iterate over all your objects and call getDisplayText() on all of them.
Hope this helps :)

Parsing RSS XML feed into a listview

Getting straight to the point, I'm new to this and trying to use this https://github.com/matshofman/Android-RSS-Reader-Library library to parse an rss feed and put all that into a listview. (I don't mind if you give me an alternate solution to this though. I picked this simply because it looked like it would be easier to understand and less work....)
MY problem is that I'm not sure how to do it, and when I read up on it on google it's either darn confusing or Eclipse says there's something wrong with it and gives me an error.
So :: can someone please explain to me, how I should go about doing this. I understand how to put a URL into this feed, and generally how listviews work,
but I'm getting stumped on passing the data that the library extracts from the feed and passing it into my listview. I don't have a clue how to write out java code that tells it to take data that this library passed and say, put it into this string.
I'm also confused about how a listadapter / adapter works. I think I understand how to write it, but all my past attempts have given me errors and I'm not sure what's the problem there.
It would also be nice if someone could explain how the list_layout thing works out. By that, I mean when you create a new xml file and define how one row of the listview looks, but I don't see how it gets linked up with the main xml file with a single listview.
Thanks for helping me out - it's a school project....
(btw, please be simple with the explanations. I think the main problem i'm having is that a lot of the tutorials like using very technical language, and it ends up not getting the point across to me. Even if you give me a chunk of code, and tell me all that parses my url, that's all i need.)
A ListView needs an Adapter to know what the data is and how much of the data there is. The ListView asks the adapter for a view to display a single item using the adapter's method getView(....). In this method you should inflate your view (the item) using the getLayoutInflater().inflate(..). You then get a view for which you can get the specific sub-views by using the findViewById(...) method on that view. Of each sub-view you set the value of a part of your item.
In order to avoid having to inflate a view for each and every item the ListView recycles item views whenever possible, therefore the method getView(...) receives a view, if that view is not null you can use that view instead of inflating a new view.
While reading your data, or when you have read all your data, you need to tell the adapter that the data has changed, which then tells your ListView that it needs to redisplay data. You tell your adapter by calling notifyDataSetChanged().
There is a Google I/O session on the ListView, maybe that might be interesting to watch: http://www.google.com/intl/nl/events/io/2010/sessions/world-of-listview-android.html
In you android-sdk under /samples/android-xx/ there is a sample called "XmlAdapter" which contains a RSS-feed activity. Could be a helpful alternative.

ListView vs Nested Layout [Pros/Cons]

I'm working on an android application and I have run into a decision that I can't decide on one way or another. I was hoping someone could shed some light on the common practices and any theory to back them.
Here's the issue:
I want to have a list of items with images and text like table...
Country -.-.-.-.-.-.- Pop -.- SqrMi
[img] US -.-.-.-.-.-.- xxx -.-.- xxxxx
Now the debate I'm having is whether or not to use a listview to display the data or instead nest a layout that is scrollable and just display the data that way.
Why would I even consider a nested layout? Flexibility. I can design the dividers, possibly throw in a couple graphs above/below the list. Vary the height of each of the list items (like what if I want to show a graph in one list item and no graph in the next), etc.
I'm not sure if I'm breaking the generally accepted methods here. Can anyone shed some light on this?
The only major problem you will have when using nested layouts is memory consumption. ListView reuses its children so it doesn't create more children then it can display at a single moment. This description is a little simplified but I hope it shows the purpose of the ListView. Also ListView implements its own fast scrolling so it doesn't have to measure all its children to determine its size. And ListViews use adapters to populate themselves with data. It's a nice and convenient way to obtain data.
On the other hand if you create a ScrollView with a lot of views inside it'll be slow, difficult to populate with data and it'll require a lot of memory to create all of the child views even if you don't ever see some of them.
And about flexibility of the ListView. You can store different types of view in a single ListView and you'll be able to vary the height of each of this views.
All the things you have described for your "nested layout" is achievable with ListView. Just use different types of rows, use headers/footers in adapter.
And as already Pixie said, ListView is much more memory efficient and faster.

Categories