I have been playing around with Android for a couple of weeks and am
finally getting somewhere with a small application I have written
which downloads Yahoo traffic data according to the current GPS
position and a set radius. In the process of this I have created a
content provider which holds the current traffic data. This was based
mostly on the Notepad example in the SDK. The main activity is
subclassed from ListActivity and the row layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="@+id/title"
android:textSize="16px"
android:textStyle="bold"
android:layout_width="140px"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/description"
android:textSize="12px"
android:textStyle="italic"
android:layout_width="200px"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
What I wanted was to be able to use "managedQuery" and point it at my
custom adapter which would handle populating the title, description
and ICON columns according to the values in the content provider.
The example here has a good explanation of the custom adapter but uses
a List to provide the row objects before mapping the type to a weather
icon.
http://mylifewithandroid.blogspot.com/2008/04/custom-widget-adapters.html
I wanted to subclass SimpleCursorAdapter which would let me use a
"managedQuery" to handle all the list stuff. I haven't seen this
anywhere else (I apologise if this seems blindingly obvious). The
secret to making a custom view for the list row is to override the
"getView" method and use the current "Cursor" to access the data
fields for the row. Here is my class:
public class TrafficDataAdapter extends SimpleCursorAdapter
{
private Context context;
private int layout;
public TrafficDataAdapter(Context context, int layout, Cursor
c,String[] from, int[] to)
{
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
/**
* Custom view translates columns into appropriate text, images
etc.
*
* (non-Javadoc)
* @see android.widget.CursorAdapter#getView(int,
android.view.View, android.view.ViewGroup)
*/
public View getView(int position, View convertView, ViewGroup
parent)
{
// Cursor to current item
Cursor cursor = getCursor();
ViewInflate inflate = ViewInflate.from(context);
View v = inflate.inflate(layout, parent, false, null);
TextView titleControl = (TextView) v.findViewById(R.id.title);
if (titleControl != null)
{
int index = cursor.getColumnIndex(TrafficData.Strings.TITLE);
String title = cursor.getString(index);
titleControl.setText(title);
}
TextView descriptionControl = (TextView)
v.findViewById(R.id.description);
if (descriptionControl != null)
{
int index =
cursor.getColumnIndex(TrafficData.Strings.DESCRIPTION);
String description = cursor.getString(index);
descriptionControl.setText(description);
}
ImageView typeControl = (ImageView) v.findViewById(R.id.type);
if (typeControl != null)
{
int index = cursor.getColumnIndex(TrafficData.Strings.TYPE);
int type = cursor.getInt(index);
typeControl.setImageResource(TrafficData.getTypeResource(type));
}
return v;
}
}
The helper method "TrafficData.getTypeResource(type)" takes the type
value and returns an image resource which is an icon to denote the
traffic event type (accident, roadworks etc).
public static int getTypeResource(int type)
{
switch (type)
{
case ACCIDENT:
return R.drawable.accident_panel_slight;
case RESTRICTION:
return R.drawable.restriction_panel_medium;
case ROADWORK:
return R.drawable.roadwork_panel_severe;
case TRANSPORT:
return R.drawable.transport_pane_medium;
case VISIBILITY:
return R.drawable.visibility_panel_slight;
case WEATHER:
return R.drawable.weather_sunny;
}
return android.R.drawable.unknown_image;
}
The call to set the adapter from the list activity is:
mCursor = managedQuery(getIntent().getData(), PROJECTION,
null, null);
TrafficDataAdapter trafficAdapter = new
TrafficDataAdapter(this,
R.layout.situation_row,
mCursor,
PROJECTION,
new int[] {
R.id.title,
R.id.description});
setListAdapter(trafficAdapter);
I hope this helps someone else.
Al
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---