Android - Does SimpleCursorAdapter allow multiple layouts like BaseAdapter? - android

I know you can create a custom Adapter extending BaseAdapter and create various layouts which can be inflated depending on which row the AdapterView is at..
But is there any way to get a simple amount of customization with a SimpleCursorAdapter?
Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.
Will SimpleCursorAdapter do? Or are there any elegant solutions for this?
Cheers

But is there any way to get a simple amount of customization with a SimpleCursorAdapter?
Just like BaseAdapter, you can extend CursorAdapter or SimpleCursorAdapter to do your customization.
Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.
If you are only alternating a minor thing like row color, you can simply override bindView() and check if(cursor.getPosition() % 2 == 0) (or == 1) to set the appropriate background color.
If you are using different types of or numbers of Views in each layout, you need to override getViewTypeCount() and getItemViewType(). Then use getItemViewType() in newView() to load the appropriate layout and in bindView() to display the appropriate data.

Related

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.

Implement a list with section divider in android

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.

How to change values in second layout in same Activity?

My scenario is that I have a activity which shows the cursor stored in SQLite DB. The main layout contains textview at top and a listview. And then I use simplecursoradapter to populate cursor into listadapter and put listadapter into listview. simplecursoradapter use another layout. The problem now is that when I use simplecursoradapter I bring three columns into listview, example: item name, date and price. That is ok if I don't change these values.
Actually I want to add some string to price and form new string such as currency sign. According to my understanding we only can setContentView for one layout not two layouts.
I also tried to populate a new layout and set value but failed
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View textEntryView = inflater.inflate(R.layout.itemlist, null);
TextView price = (TextView) textEntryView.findViewById(R.id.price);
price.setText(currency + c.getString(4).toString());
Even there is no problem in syntax and run apps. But when I run the app and check listview, the price still show price only without adding currency sign. I only can add currency sign under the main layout not the second layout used in simplecursoradapter.
In fact, currency is chosen in user preference, and I use sharedpreference to retrieve its value and add to price value in cursor. It seems that simplecursoradapter is using different layout, so cannot do that.
Does anyone has ideas about this case ?
I would be appreciated if methods and codes are provided for similar approach.
Thanks !!
I'm not sure I follow your question 100%, so this might not be exactly the answer you're looking for.
If you want to have control over how the layout of items look in a list view, you should make your own custom implementation of ArrayAdapter:
A ListAdapter that manages a ListView
backed by an array of arbitrary
objects. (...) To use something other
than TextViews for the array display,
for instance, ImageViews, or to have
some of data besides toString()
results fill the views, override
getView(int, View, ViewGroup) to
return the type of view you want.
If you just google for custom arrayadapter example, you should find enough examples showing you how to implement this. Two such examples are:
http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
Good luck :)
Finally I used bindview to solve my problem. In my question I already mentioned that I use simplecursoradapter to get data from SQLiteDB not array. I know arrayadapter works but it doesn't work for SQLite efficiently. Before I extended baseadapter to design my own to add custom views on textviews. It works but too slow if there are lots of data. So simplecursoradapter is more efficient.
I extended simplecursoradapter and override bindview for my own purpose. It works for what I need.
Thanks for your help and link for arrayadapter

Android listview groups

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.

ListView and dynamic row sizes

I'd like to use different row descriptors (from the XML layout resource) in one ListView. Can I and how?
Also, I'd like to be able to programmatically change the size of the rows, based on the data in each row, in a ListView. Can I and how?
Thank you in advance--I LOVE stackoverflow.
M
I'd like to use different row
descriptors (from the XML layout
resource) in one ListView. Can I and
how?
Step #1: Override your Adapter class and implement newView()/bindView() (or getView() if ArrayAdapter)
Step #2: Inflate the rows you want when you want them from the layouts you want
Step #3: Override getViewTypeCount() and getItemViewType(), returning appropriate values, so Android knows to use different object pools for each distinct type of row
This book excerpt covers #1 and #2, though for only one type of row. I don't have any samples handy for multiple types of rows, sorry.
Also, I'd like to be able to
programmatically change the size of
the rows, based on the data in each
row, in a ListView. Can I and how?
Put in bigger stuff in the row. Heights of rows are determined by their contents.

Categories

Resources