I have a ListView and it should have four different types of items. I searched for it, found different solutions that this is possible and tried to do my stuff just like the other persons did. I've created different ViewHolders (one for each type). When the ListView appears for the first time everything is fine. But when I begin to scroll I get a classCastException.
The reason is pretty obvious:
holder = (FeedViewHolder) convertView.getTag();
My adapter tries to get the ViewHolder by getting the tag from the convertView if it's not null. But this ViewHolder seems to be the ViewHolder of the previous item so it can't be casted.
I search StackOverflow and Google but all the examples seem to work as they use the same or pretty equal ViewHolders for their different items. But my Objects really differ from each other and need different ViewHolders. Can anyone help me to solve this?
Ok a collegue just helped me:
I hadn't implemented the method getViewTypeCount. That was it. The Android system didn't knew that there different view types because I hadn't defined them. Thanks anyway to dmaxi!
Define item view types for the ListView and cast the ViewHolder based on the actual view type as the code shows in this answer: Creating ViewHolders for ListViews with different item layouts
Related
This question already has answers here:
Android Recyclerview vs ListView with Viewholder
(7 answers)
Closed 3 years ago.
I was reading about the difference b/w recyclerview and listview and found out that recyclerview is faster than listview.
I tried to search online but not found any satisfactory answer I know it is used ViewHolder pattern and Notifying adapter but what does it does intearlly so it is faster?
Recycler View you could say is an efficient way to create list of views.
If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.
Recycler View by default does this, where as List View by default doesn't do.
There are some differences between these two views.
ListView is a bit heavy and it has a lot of responsibilities. Whenever we have to handle the list, such as to configure it in some way, the only way to do this is through the ListView object or inside the adapter.
A lot of bad things in the ListView were fixed or changed in the RecyclerView. It’s more efficient by default, the layout is separated and we have more possibilities over the data set inside the adapter.
These are some crucial differences between ListView and RecyclerView:
1 ViewHolder
The ViewHolder pattern allows us to make our list scrolling act smoothly. It stores list row views references and, thanks to this, calling the findViewById() method only occurs a couple of times, rather than for the entire dataset and on each bind view.
The RecyclerView’s adapter forces us to use the ViewHolder pattern. The creating part (inflating the layout and finding views) and updating the views is split into two methods — onCreateViewHolder() and onBindViewHolder().
The ListView, on the other hand, doesn’t give us that kind of protection by default, so without implementing the ViewHolder pattern inside the getView() method, we’ll end with inefficient scrolling in our list.
2 LayoutManager
The LayoutManager takes responsibility for layouting row views. Thanks to this, RecyclerView doesn’t have to think about how to position the row view. This class gives us the opportunity to choose the way that we want to show the row views and how to scroll the list. For example, if we want to scroll our list vertically or horizontally, we can choose LinearLayoutManager. For grids, it is more suitable to choose the GridLayoutManager.
Previously, with the use of the ListView, we were only able to create a vertical-scrolling list, so it wasn’t that flexible. If we wanted grids on our list, we had to choose the other widget for that — GridView.
3 ItemDecoration
A duty of the ItemDecoration is simple in theory – add some decorations for the list row views – but in practice, it’s that simple to implement if we want to create a custom one. In this case, we should extend the ItemDecoration class and implement our solution. For example, the RecyclerView list has no dividers between rows by default and it’s consistent with the Material Design guidelines. However, if we want to add a divider for some reason, we can use DividerItemDecoration and add it to the RecyclerView. In case we use the ListView, we have to figure out rows decorations by ourselves. There is no helper class like ItemDecoration for this widget.
4 ItemAnimator
The last but not least component of RecyclerView that I want to mention is ItemAnimator. As we can expect, it’s handling row views animations like list appearance and disappearance, adding or removing particular views and so on. By default, RecyclerView’s list animations are nice and smooth. Of course, we can change that by creating our own ItemAnimator, which is also not that easy. To make it easier, we should extend the SimpleItemAnimator class and implement the methods that we need (just add animations to a ViewHolder’s views). To be honest, implementing animations on the ListView was a pain. Again, we had to figure out how we want to handle them.
5 Notifying adapter
We have a couple of cool notifiers on the RecyclerView’s adapter. We are still able to use notifyDataSetChanged() but there are also ones for particular list elements, like notifyItemInserted(), notifyItemRemoved() or even notifyItemChanged() and more. We should use the most appropriate ones for what is actually happening, so the proper animations will fire correctly.
Using ListView, we were able to use just notifyDataSetChanged() on the adapter and then had to handle the rest ourselves, again.
Because of ViewHolder Pattern.
Thats was the simplest answer. Now for some details.
What recycler view does is what it's name indicates "Recycle", yes it recycles items, and it does it with the help of ViewHolder Pattern.
By Using ViewHolder we do-not need to call findViewByID() every time we go through getView()method. The reference for all rows are stored in-memory. This increases the performance significantly, as findViewByID()is a heavy process.
Hope this clears your confusion.
i'm quite new to app developing and I am wondering about some good design patterns when it comes to adapters for ListViews.
I don't have any specific code (at least not simple and clear code) to accompany the question, but anyway, here is the situation.
I have an Activity in which I want to show any List of Books.
I made a CustomAdapter for this, based on layout called "row.xml." This xml includes a TextView and an ImageView.
I create my CustomAdapter in this Activity as follows:
customAdapter = new ListAdapterFullList(this, R.layout.row, bookList.getList());
Now i want to reuse this Activity to show a List of Books, but the layout needs to be different this time! It should be "row_alt.xml". Instead of a TextView and an ImageView, this list now needs to display a TextView and a RatingBar.
The problem that i am facing here is that obviously I want to reuse the Activity (since what i need to display is still a List of Books and all the other behaviour is the same), but I don't know how to handle the adapter. I can't just use this:
customAdapter = new ListAdapterFullList(this, R.layout.row_alt, bookList.getList());
because this ListAdapterFullList is not implemented to handle a RatingBar instead of an ImageView.
How can i bypass this problem? Do I need to create a different Adapter to show this layout? If so, then I will need to modify my existing Activity also, although I do not know how.
Btw, I read a lot about problems where different layouts need to be used for each row in a ListView, but this is not what I am struggling with. I want to reuse an adapter with a completely different layout for the Adapter. The rows themselves are always the same.
Thanks for the help, and apologies for the possible confusing question. You can always ask for more info!
If row.xml and row_alt.xml only have two different widget, I suggest that you create two different Adapter. If really there is a common behavior between this two Adapters, create a parent class that will implements the common behavior and make your two adapter extends this parent class. Then each Adapter implements its own [getView()](https://developer.android.com/reference/android/widget/Adapter.html#getView(int, android.view.View, android.view.ViewGroup)) method.
Then for further optimizations use the ViewHolder Pattern.
I also suggest that you take a look at RecyclerView which is the new version of ListView and don't need the ViewHolder Pattern
I've recently searched and got some ideias for my problem, but still couldn't solve it.
In my Android App I have a CursorLoader bringing information from SqLite3. When I display this information, according to the type of it (this type is saved in DB), I will have a different ViewGroup.
Just to clarify a little more, I am using the concept of cards, each card has different layout and also different actions.
The number of cards is huge and I do need it to be scrollable.
I could, so far, switch the XML layout to be inflated on each row of my ListView.
What I couldn't do is having each card/row with different methods. I was wondering if extending the View base class or even the ViewGroup is a good solution?
Thanks in advance.
Here is the problem i am facing. I am developing a custom list view in which a single list view item is consisted of multiple text and image views. But depending on the item properties it sometimes needs to have 4 TextViews sometimes 1, sometimes 6.... the number is dynamic and i am having trouble figuring out how to developed a proper view holder pattern that will work.
I can not solve the problem by creating a number of TextViews and hiding them and displaying when necessary, since i dont know how many of them are necessary per item.
And inflating them dynamically and adding to the layout every time in getView is disastrous to say the least :(
Is it a viable solution to have an array list of Views in the holder and then work something out?
I'd recommend using the adapters view types: getItemViewType() and getViewTypeCount()
The number of view types depends on how many permutations are needed to represent all the varying ways an individual list item will look.
So if there are 4 different UI looks, then you'd have 4 different view types. You'd then have a ViewHolder representing each type. Then in the getView(), you just detect which type is represented by the position and inflate/populate as needed. You won't need to worry about hiding different portions of the UI to change up the look. The adapter will ensure you are only given a recycled view for the proper type.
For a nice detailed explanation of using the view type, check out this post.
I'm new at Android programming and I'm stuck in a situation for two days that I can't seem to solve.
I've got a ListActivity that should be populated with two different views that has almost no simularity. Untill now I've used MergeAdapter and it worked fine but my outcome was like this:
Adapter1,Adapter2. and I didn't have the ability to change the order of the items coming to the list.
My question is: is it possible to create an adapter that is holding items of two views and not adapters of two views so I'll have my items sorted by the way I input them?
For simplicity sake, I got an ArrayList of those two items and each has "int position" so I'll be able to insert them into the list sorted by position.
EDIT: I've tried extending BaseAdapter but once again I need two adapters as I've read online but if I do so, I won't be able to control the place of an item on the list.
I hope I'm more clear this time.
Welcoming any response.
Thank you.
You can subclass BaseAdapter and utilize the following methods:
getItemViewType(int position)
getViewTypeCount()
getViewTypeCount() should return the number of different row types your adapter supports. getItemViewType() is where you implement the "decision" of which view type a particular row should have. If, for example, getViewTypeCount() returns 3, then getItemViewType() method should return 0, 1, or 2.
You can use this inside of getView to inflate/create a different layout for different row types.
EDIT:
Since it's a custom adapter, you can implement it in whatever way makes sense for your data. If you can create a data structure that works for what you want, then just make the adapter able to work with that. In the worst case, you might just have an array (or list) of Objects and have to use instanceof to do the decision work.