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
Related
My problem is simple: I need to make a layout similar to android.R.layout.simple_expandable_list_item_1 which can fit more than one textview in the layout, as well as show the little '>' symbol that indicates that the tab is expandable.
How can this best be accomplished? My first thought was to use the layout as a background of a linear layout with two textviews in it, but I can't seem to make that work.
Vogella has a pretty good tutorial on how to achieve custom list views through Adapters.
Basically, what you do is you create a Layout with the looks you want for the list items and then you extend an adapter (ArrayAdapter for instance), see section 4. Then, in your activity, you create an instance of your Adapter implementation and populate it from an Array containing the data for the list.
Also have a look at the Developers site for how to make the list scroll smoothly with the ViewHolder pattern.
And if you feel you need more info on the expandable part maybe the hive can help.
How can I display information in the following format? What controls should I use, listView or RecycleView?
Note that the question isn't about this particular activity and how to use it. It's about how to show the information in the same format and how to create the layout for it.
That is a ListView. You would just need a ListAdapter/ArrayAdapter to fill it and that's about it.
You can make exactly the same thing using the layout simple_expandable_list_item_2 in the SimpleAdapter of a ListAdapter (here is how you do it : Displaying kind of static data in ListView)
RecyclerView is of course much better in terms of performance and flexibility. However, ListView is enough in many cases like this one.
I'm trying to make a android app with a scrollable list like this one...
When I made this app in iOS, I used a UICollectionView. What`s the best way to make this in android studios?
I tried using a list view, but I can't seem to customize it to my needs?
Any Ideas?
ListView was a great way to start and it is customizable to your needs.
However I would recommend to use RecyclerView which works almost on the same principle as ListView but it is a newer concept in Android. It handles the usage of the ViewHolder pattern for you which makes everything super easy.(With ListView, you would've had to implement your own ViewHolder pattern)
All you need to do is to have the RecyclerView in your activity/fragment as the view to hold your data. Then, the key component is to implement the RecyclerView's Adapter which will handle the inflation and setup of each list item.
Here is a really great and short tutorial to get you started with RecyclerView.
If you're done with that here is a bit more advanced video material on RecyclerView from Dave Smith explaining a lot of ways on how to understand and use RecyclerView in your apps.
A ListView fed by an ArrayAdapter of custom objects can do the job.
You might have a class that contains the text and the image of a single item.
Then you can create an ArrayList with items of that class, where you put in all the data.
This ArrayList can be used as dataset for a custom ArrayAdapter, which overrides the getView()-method.
The getView()-method must inflate the xml-layout for each item and attach the data from the ArrayList to the inflated view.
You might want to implement the Viewholder-pattern to achieve a good performance and avoid unnecessary inflations and getViewByID()-calls.
You can also use the new CardView, which can make it a lot easier, since it is made for the new material design in android 5, which looks similar to your item list.
I'm new to Android development and I was coding with ListView with a custom adapter. So far so good, then I wanted to use the same adapter with more than one listview, is that ok?
And when I override the method getView(), then I use the same resource to show the views (eg. R.id.show_view). Can I use different layouts in the same adapter? I don't know how to accomplish that.
I dont have the code here, sorry, it's more a question of whether it's a good practice to use the same adapter (eg. ArrayAdapter) to match various ListViews.
You can reuse class, but no instance of adapter. And you can create different views for every entry. But for sake of performance, you shall reuse view supplied to getView() whenever possible
I need to create an activty that will show an information table. The problem is that I want to display the information like the following image. Each row has to be like:
I dont really control the layouts yet... so if someone can guide me a little I would be really appreciated.
All of this has to be in code.
Thanks!
It sounds you you want to make a list with custom list items.
Typical approach would be to use a ListView with a custom adapter. You can derive an adapter from, say, ArrayAdapter and override its getView. There, you can use LayoutInflater.inflate to load an XML layout for the list item, and populate it with whatever data. If you really must do it "all in code", however, you can do things like new RelativeLayout() and new TextView() and add them to the parent view in this method. As for the layout itself, a RelativeLayout most likely works best to arrange everything.
Here is an example of how it all fits together.