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.
Related
I want to have in my GridView first item different from the others. The gridview's adapter can be either adapter extending from CursorAdapter or ArrayAdapter. It is depending on from where are images path taken - db, or arraylist.
For now everything works fine, but I want to have first element different from the rest. The first element, no matter what is the adapter, has to be empty element which will be button from where I can add extra elements. The image in first element has to be from resource, while the images for rest of the elements are form uri.
Also, there is emptyView of this gridView set. I've tried adding first element into arraylist at the very beginning, but then empty view is not shown. Also, I don't know how it will work with content from DB. To be honest I haven't got any more idea, and also I cannot find anything in Google.
Do you know any way I can add this first view?
I need it to work on API10 and above.
You have 2 options
in the adapter, you can check if position is 0 than inflate the other view.
More complex but seems like better one for you is to override (Assuming you are using ArrayAdapter) the methods:
getItemViewType(int position)
getViewTypeCount()
You can find nice and friendly example here
--- Edit ---
To show your empty view you need to be sure that:
Your View inflated correctly.
you called mGridview.setEmptyView(view);
There are no items to show in the adapter, that means that in your grid view adapter the function getCount() returns 0. Note that this function should be implement with the correct logic after you implements the 2 methods I've mentioned above.
Hi how can i implement a list with section divider like the one on android's building blocks lists
Can you point any articles or tutorials to achieve this?
I implemented a possible version here:
http://steprobe.wordpress.com/2013/03/29/google-building-blocks-style-listview-for-android/
There are many tutorials to get grouped lists. The trick is to look for "adapter" and not "list". For instance, this one: http://android.cyrilmottier.com/?p=440
Each row in your list can be totally separated in terms of layout from others. So if you got 3 rows on your list, you can have each one can look totally different. You need to write your own adapter (i.e. extending ArrayAdapter), override getView(), getViewTypeCount() and getItemViewType(). Then for each row your getView() shall do any logic you want, inflate any layout you want and return that View to the list.
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.
I have Activity for displaying search results. It extends ListActivity. I need to show search results in listview grouping several items. So, how do I add grouping to my listview?
You give your ListView a ListAdapter that knows how to do the grouping.
This could be a matter of overriding getViewTypeCount() and getItemViewType() in the adapter to describe which positions get which sorts of rows, plus modifying getView() (or newView() and bindView() for CursorAdapter) as needed.
Depending on what you are starting with, you might be able to use something like my MergeAdapter instead.
I want to generate a ListView that has some dividers between some of the entries, like it can be seen in some of the property sections. See the example below. I try to generate a List that consists of some textviews followed by one of the fancy dividers explaining the next part of the list and then again some text views. How can this be done? I thought about creating different views to add to the list? Is this the way to go?
I got a solution. I don't know if it is the best one.
I use a custom adapter derived from ArrayAdapter for the list as described in this tutorial. In the adapter class I check if the position in the getView method is a normal row, then I inflate the row layout. If it is the first row from a new group I inflate a headline layout that is a normal row plus the group headline above it.
If you don't want to mix the header into one of your rows. Consider the following solution:
You can overwrite the two methods getItemViewType and getViewTypeCount.
You now have a list that can display different rows. You need to check the expected view type for the item in the getView Method and inflate different layouts depending on it.
The list will handle the recycling for you in a way that it will return only correct recycle views to your getView method, this means if the recycleView is not null it can be used to display your current cell.
You can use my SectionedAdapter, if GPLv3 is acceptable (licensed that way due to some upstream code). You can use my MergeAdapter, if you need something more flexible and with a less-limiting license (Apache 2).
I think you might be looking for android.widget.ExpandableListView
http://developer.android.com/reference/android/widget/ExpandableListView.html
I'm also interested in an answer to this. There must be a more straightforward way to do this.
In looking at the Adapter, there's a method, Adapter.getItemViewType(int position).
ListView defines a return value, ITEM_VIEW_TYPE_HEADER_OR_FOOTER which indicates if the returned item is a header or footer.
I haven't tried it, but I assume if you create your own Adapter and return an item with the type indicating it is a header or footer, that the ListView will display it appropriately.