Android list view to display different data sets - android

Is there a way to get the listview to display data provided from another array list without having to create a new adapter and setting it?
I ask because the way I have it now prevents me from maintaining the scroll position of the list view

I assume you are talking about different datasets and different display per dataset.
If this is the case, you can use one of few solutions:
feel free to use either RecyclerView with multiple ViewHolder classes for it.
Try unifying the dataset representations with a common parent class and use one Adapter while overriding the getViewTypeCount to return 2 and then inflate different views for each type.
Try using MergeAdapter.

Related

Android most efficient Table Layout

So I have a few arrays of data I would like to display in an activity without having like 15 text views with unique ids. Is there a code efficient way to make a Table layout or something like it where I could feed in data and it would automatically place it in there respective text views? Thanks!
I think you can achieve that by using RecyclerView (with a GridLayoutManager). Have a look at this answer.
If there are only TextViews and you don't want a specific layout you can use SimpleAdapter, if you want to modify the layout you have to extend RecycleView.Adapter (there is an example in the answer above).
You can add/remove items into/from a List and use DiffUtil that
can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
There are a lot of tutorials about using this class. Have a look here or here.
Or you can use the notifyItemChanged() method:
If the list needs an update, call a notification method on the
RecyclerView.Adapter object, such as notifyItemChanged(). The layout
manager then rebinds any affected view holders, allowing their data to
be updated.
LE: There are some libraries available. Here is a list:
https://github.com/evrencoskun/TableView
https://github.com/HYY-yu/TableRecyclerView
https://github.com/Cleveroad/AdaptiveTableLayout
https://github.com/celerysoft/TableFixHeaders

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 - ListView Adapter with items of two views

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.

Complex Items in a Listview

I have a ListView where the view for each item is a string (the name of the item). But I have to associate a lot of other data with that item: price, size, weight, tax, etc. So, I'm of creating a new Java class called Item, and then an object for each item in the list.
I don't know which is the bext way to implement this. There's two obvious choices:
1) I can simply create the data structure outside of any Android Views, and then write a method called UpdateList() which takes the name of each item in this data structure and puts it in the ListView. The problem with this is that some of the data is duplicated twice (the original data structure, and the adapter for the ListView) and when you duplicate data, bug potential arises.
2) Or, I can somehow associate the data structure directly with the adapter for the ListView and have it figure out how to display the name for each ListView entry that is displayed. The advantage here is that you only have a single data structure. But I don't know if this is possible in Android, or very complex.
Which is the preferred way to do this with Android apps?
You would be better with the ListView and the Adapter option, You would need to create a custom ArrayAdapter to populate a ListView from this objects the way you want.
The advantage of this technic is that you gain a Views recycle mechanism that will recycle the Views inside you ListView in order to spend less memory.
In Short you would have to:
1. Create an object that represents your data for a single row.
2. Create an ArrayList of those objects.
3. Create a layout that contains a ListView or add a ListView to you main layout using code.
4. Create a layout of a single row.
5. Create a ViewHolder that will represent the visual aspect of you data row from the stand point of Views.
6. Create a custom ArrayAdapter that will populate the rows according to you needs.
7. Finally assign this ArrayAdapter to your ListView in onCreate.
You can get an Idea of how to implement this by reading this blog post I wrote:
Create a Custom ArrayAdapter
Just use the adapter. It's much cleaner. Then you can retrieve the info you need when you display the list item with getView(). See this example.

Categories

Resources