tags:

views:

177

answers:

2

I'm building an app that has a ListActivity and the view for each item has a progress bar. I've been able to bind my data to the TextView, but I can't seem to figure out how to bind it to the max and progress of the ProgressBar.

This is a simplified version of my view:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_alignParentLeft="true"/>
<ProgressBar android:id="@+id/progress_bar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text1"
    />
</RelativeLayout>

and I'm using the same method as the Notepad tutorials to put my data into the views:

String[] from = new String[]{MyDbAdapter.KEY_AMOUNT};
int[] to = new int[]{R.id.text1};
SimpleCursorAdapter list_adapter = 
    new SimpleCursorAdapter(this, R.layout.item_row, myCursor, from, to);
setListAdapter(list_adapter);

Adding MyDbAdapter.KEY_AMOUNT and R.id.progress_bar to the arrays throws an exception (java.lang.IllegalStateException: android.widget.ProgressBar is not a view that can be bounds by this SimpleCursorAdapter). Is there some kind of @... that I need to use for max and progress to make this work?

+2  A: 

You will probably need to override bindView() (and, possibly, newView()) and do the binding "by hand" in Java code. SimpleCursorAdapter knows how to handle TextView and children automatically, but everything else needs help.

CommonsWare
A: 

You have to create a new class that implements SimpleCursorAdapter.ViewBinder and override the setViewValue(View view, Cursor cursor, int columnIndex) function to implement what you want your progress bar to do with the returned cursor data.

Then you call list_adapter.setViewBinder( ViewBinder) and pass your new ViewBinder object in there.

Hope that helps

wnafee