Correct approach on using RecyclerView? - java

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.

Related

How to get all holders from RecyclerView?

I am trying to get all holders from an recycler view in android.
The problem is that with my code i can only get only the holders from the visible elements(recyclerView.getChildAt(i)).
How can i get the elements that are recycled?
EDIT
Since i see that the question wasn't very clear, here is what i'm trying to do.
So i have a quiz, and i used a recyclerView for the questions. For every question, i have another recyclerView, in which i have the possible answers like a single selection.
So, at the end of the test, i need to get the answers from every question not just the ones visible.
I did something and i stored every questionHolder in a list but now i think that the usage of recyclerView is wrong here and doesn't provide any advantage.
The whole point of the ViewHolders is that they are recycled. I.e. when an item scrolls offscreen the same holder may be used for an item that is appearing on the other end.
Items that are recycled are, well, recycled. So you can't get to them.
This seems like an xy-problem. You may have to rethink what you are trying to do.

Android - Switching between Activities and Screens with same xml file

I'm building an app for educational purposes, with a lot of questions and challenges to the user to solve. But, it would be a HUGE amount of screens if I create a lot of questions and one xml file and activity for each one, as I want to have different kinds of questions, where the user should be able to write the code, select a block of code or select the correct answer about theory and stuff. Is there a way to use the same model of screen(xml file) for one specific kind of question? Like, using one model to all the screens and questions where the user should select the correct answer, and another model to another kind of question..
p.s: Yes, i'm kind newbie in Android
Thanks! :)
I would recommend using FragmentStatePagerAdapter
You only needs one layout and every time a fragment looses focus, it is destroyed automatically.
It is the same as adapters. You just create the visible views and 2 more. In this case you will have the visible question and one on the left and one on the right.
Let me know if you need any help.
You can define all kinds of questions, such as checkboxes, radioButtons, Edittexts and etc. in a xml file and, if necessary, visible or hide them.
Use Fragments to display questions in your activity.
In short, Fragments are mini-Activities with their own layouts, so each different type of question should have a corresponding Fragment. That should be exactly what you need.
For example, you can create a fragment_radiobox and a fragment_codesnippet in your project. In first one you will include a TextView for your question and a few checkboxes for an answer, and in the second one you'd have an EditText field to write some code. You can call any of those two fragments in your main activity depending on which question you want to display.

Handling empty lists - better to use ListFragment or Fragment+ListView?

I have some doubts. I'm trying to make an application with lists. I made one but with a ListAdapter extends BaseAdapter and ListView.
Then I realized that there is also ListFragment. And with the fragments you can easily display messages "empty list". What I want to do is a list of items and each item associated with this single_item.xml for example. I have seen several examples, but there are differences between them and me is not good either choose from.
Which is the updated method to do this and has the best performance? Are ListFragment better than ListAdapter extends BaseAdapter?
Thanks in advance.
ListFragment essentially provides shortcuts for interacting with a Fragment whose layout contains a single ListView (and nothing else, by default). The ListFragment does not provide any performance advantage, as far as I know. You can inspect the source code here if you would like to know more about how the class functions.
My preference is to avoid ListFragments - I find them inflexible if the layout of a screen ever needs to change, plus they use slightly differently-named APIs that can be somewhat fiddly to remember (onListItemClick, vs the onItemClick callback of a standard AdapterView, for example).
Here is a good article that describes how to handle empty ListViews. You are looking for the setEmptyView(View) method of ListView. This allows you to specify the view to be displayed when the ListView is empty.

dynamic layout based on dynamic data in android

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.

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