Using a custom Adapter for multiple ListViews - android

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

Related

How to implement Android Multi-item listview like iOS UITableView

Now I am implementing chat screen in an Android App.
the use of the adapter pattern of ListView seems to be the best one cell.
You know you need a cell of a different type of user messages, photos, videos, dates, announcements, etc in chat screen.
Conventional methods that I know of is two things.
In getView() method, create each time a new type of cell.
First of all, put all the item and ,in getView() method, adjust the UI layout by VISIBLE/GONE options.
The second method is better i thought, but this method is still a waste of memory and complexity to implement.
For UITableView of iOS generate multiple queue by the number of type of cell. It is efficiently in Multi-Item ListView.
there any easy way to implement as iOS?
Use RecyclerView instead of ListView.
Create xml file for each item view. Create ViewHolder for each file xml.
Inside Adapter of RecyclerView override getItemViewType() return view type correspond with it's position. Override onCreateViewHodlerwhich return ViewHolder correspond with view type. Finally, override onBindViewHolder bind your data to your layout.
Consider using this library JSQMessagesViewController its very flexible and scalable.

how can I use one custom adapter class for multiple screens in android?

How can I use same adapter class for multiple screen(different task) under same project.
Tasks are:-
Suppose:-
one screen has Grid view image with text
second Screen List View only text
same adapter for drawer Layout
is possible to use same custom adapter for every classes???
I would recommend not to create the same adapter in that case. Maintain different Adapters for that, Because in the future You would need different animation for both or some specific changes.for list/grid in that case you will end up writing lot of if-else in getView(), that will make your listview/gridview slower
Best way is, you need to create separate CustomAdapter for both . If you have same type of layout means you can use same Adapter. otherwise create new one

Android: reusing a customadapter when dealing with multiple layouts

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

Whats the best way to put a customized scrollable list in android studios?

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.

Android set elements of listview cell WITHOUT custom listview adapter

I want to do something like mylistview.setElementsofView(0).getElementById.setColor("black");
currently the only way I know of doing this is setting up a custom list view adapter, setting up constructors, do conditional checks to see if view is populated, then do stuff in that view
this seems very convoluted, when other parts of the listview are so easily accessible.
is there some inherited function I can access in listview to allow me to access the elements of a particular view without a custom adapter?
Thanks for any insight
The short answer to your question:
is there some inherited function I can access in listview to allow me to access the elements of a particular view without a custom adapter?
unfortunately is no.
Why do you think setting up a custom adapter is so convoluted? Just make the customized adapter class a nested class within your activity. Most likely, you'd only be looking at overriding the getView() method. In the end, you'll spend a lot less time doing this than looking for a "simple" solution.
What about
myListView.getChild(index).setColor("black");

Categories

Resources