ListViews inside ListViews - android

I have to implement something like that:
"ListViews"
I call month.setadapter(adapter) once for each year but it's not working because the last month overwrites the information of the previous last one.
Please help me regarding this.

What I normally do in a situation like this is implement it as a single custom adapter.
Derive from BaseAdapter
Set your data as a non-generic ArrayList. You can fill your ArrayList with data representing your month rows and the detail rows within each month. You need some way of differentiating them in the adapter, so wrap things in custom objects that allow you to do this. In your example above, I'd say you can iterate through your data and insert a Date object each time you reach a row with a different month than the previous data row.
Your adapter should override getItemViewType and getItemViewCount. getItemViewCount returns the total number of different view types (looks like you'll return 2 from this method).
In getView, take the position parameter and pass it to getItemViewType. In getItemViewType, get the item in your ArrayList at the current position and test it for its data type. Return a constant representing which type of data to display.
In getView, now that you have the data type to display, run code appropriate for this display - inflate the layout representing the appropriate row type and set the appropriate data on the child views of the layout.
All of this is covered in the "World of ListView" video from Google I/O 2010 that ALL Android programmers should watch at least once:
http://www.youtube.com/watch?v=wDBM6wVEO70

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

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.

Android add extra views to an ArrayAdapter that aren't from the array being adapted

I would like to display a list of items in a ListView, and display an extra view at the bottom of the list (a "Get more" button) that isn't from the original array. I thought I could just override getCount() to return the size of my array plus one for the extra item, then in getView() I would build the custom view if the position paramater is greater than the number of elements, but I get a crash in Choreographer.doCallbacks() if the count I return is higher than that number of items in the list.
Is there something I'm missing? Or is there a better solution in general?
ListView.addFooterView() is exactly what you need.
Don't use an array adapter for this. You need to write a custom adaptor. It's fairly simple to do, it only requires 3 methods to write.

Add multiple rows to ListView per item

I have a ListView that has a custom ArrayAdapter with a custom XML row.
I am passing in objects and everything words fine. However, I want to repeat each row 5 times within the ArrayAdapter. In the adapter, I would like to make minor adjustments to each and the current setup it isn't feasible to make adjustments prior to passing in to the adapter.
Is it possible to do this? I can't seem to conjure up the correct search terms to find any hints.
There are two ways i know:
1. Add repeated items to the dataset multiple times. Since you are referencing to the same object it is pretty cheap.
You can store the number of repetitions in the objects, and implement methods getCount(), getObject(), getView(), getItemId() remembering the count of repetitions.
f.e. if you have Foo object with 2 repetitions and Bar with no repetitions getCount should return (2 + 1) + 1. You also probably would like to count that values in the constructor or maybe when the data set changes to speed up ui a litle bit.

Using BaseAdapter to populate GridView

I am having some difficulty in getting my implementation of a BaseAdapter working to populate a GridView.
I've a few questions around the workings of the BaseAdapter, based on the example here: http://developer.android.com/resources/tutorials/views/hello-gridview.html
In my implementation, my BaseAdapter.getCount() method returns 130. Therefore I would have expected the getView() method to be invoked once for each count. However, as far as I can see getView() is only invoked approximately 70 times....why is this?
If getView() is used to construct the View object at each position in the grid, then what is the purpose of getItem() which returns an Object?
Lastly, I wish my grid to be 10 columns wide and 13 rows in height. To achieve this, do I just set the android:numColumns attribute to be 10?
Any help is appreciated in understanding this.
Thanks.
However, as far as I can see getView() is only invoked approximately 70 times....why is this?
It generates the views as they are rendered to the screen
If getView() is used to construct the View object at each position in the grid, then what is the purpose of getItem() which returns an Object?
getItem(int position)
Get the data item associated with the specified position in the data set.
As the description says, it returns the underlying data associated with that position as opposed to the View for the same position.
To achieve this, do I just set the android:numColumns attribute to be 10?
android:numColumns setNumColumns(int) Defines how many columns to show.
The description is quite explicit here.
BTW, this last question is one that you could easily test yourself rather than asking here...

Categories

Resources