views:

66

answers:

1

I have a ListView:

...

 <ListView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="false" android:scrollingCache="true" android:fastScrollEnabled="true" android:choiceMode="singleChoice" android:background="?android:attr/colorBackground" android:layout_weight="1" android:cacheColorHint="?android:attr/colorBackground" android:fadingEdge="none"/>

...

And a layout for the list items:

...

 <ImageView 
 android:id="@+id/folder_image"
 android:src="@drawable/icon_folder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingRight="5dip"
        android:paddingTop="3dip"/>

    <TextView
        android:id="@+id/fol_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22dip"
        android:layout_toRightOf="@+id/folder_image"/>

...

The ListView is populated from a database using a setListAdapter(SimpleCursorAdapter). For each row, I want to display an image (a folder icon). However, for two specific rows (assume I know how to identify the rows using, for example, some sort of indicator) I'd like to display different images. Let's assume that I have an undetermined number of folders and a folder icon will be displayed for each row along side the folder's name. Let's also assume that I have, for example, a Word doc and an Excel file, exactly one of each, and I'd like to display the appropriate images for each.

A: 

Akin to what stealthcopter wrote, you will need to extend SimpleCursorAdapter and override bindView() to implement your icon-selection algorithm. Here is a free excerpt from one of my books that discusses the technique.

CommonsWare
While I really do appreciate your help, your book excerpt only talks about using an array. This was not what I asked. I was looking for a code sample where a database was used. Array examples are all over place but this is obviously only practical when writing a book. Pretty much all apps would use a database to store its data rather than hard coding them into an array.
Aaron
@Aaron: "This was not what I asked." Yes, it is. The techniques for customizing an adapter are basically the same regardless of data source. The only significant difference with the `CustomAdapter` family is that you use `bindView()` and `newView()` for the two pieces of `getView()`, as was described in the excerpt linked to above (see the last two pages). The excerpt does not use a database example because databases are not yet covered by that point in the book. Also, arrays and `ArrayAdapter` are used fairly commonly in Android development, despite your assertions to the contrary.
CommonsWare
Yes, arrays are used a ton but for the ListView, I would imagine that the data usually comes from an outside data source more way more often than it is from hard coded arrays. Arrays are used in examples all the time because it's far simpler to write a self-contained code sample than deal with a database. I fully understand that. And my question specifically asked about the SimpleCursorAdapter. Either way, ultimately, my class extended the SimpleCursorAdapter and overrode the getView() method which worked perfectly. In the end, I based it on your code sample so I greatly appreciate your help.
Aaron
@Aaron: "I would imagine that the data usually comes from an outside data source more way more often than it is from hard coded arrays." -- there are more "outside data sources" than just a database, and once it's in an array, there is little difference between it being hard-coded, it coming from a Web service, or whatever. "And my question specifically asked about the SimpleCursorAdapter" -- which my excerpt addresses, except you didn't follow the instructions (see the last 2 pages), and therefore you overrode `getView()` when you were supposed to override `bindView()`.
CommonsWare
I apologize. I meant bindView(). And I was just using a database as an example of an outside data source. There are obviously a plethora of other sources such as XML, raw text files, Web services, etc. I meant outside as opposed to hard coding an array in the actual code. It is unusual for large amounts of data to be hard coded withing a method. To be fair, you do say in the excerpt that "Details of using a Cursor will be covered in the chapter on databases." I was looking for the details. :-). But seriously, I really do appreciate the help and do not take it for granted.
Aaron