I am currently using the following adapter to read from an SQLite
Database :
private static int[] TO = {R.id.name, R.id.description, R.id.address, };
private void showPlaces(Cursor cursor) {
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor, FROM, TO);
setListAdapter(adapter);
}
I then also开发者_StackOverflow have the following layout file which is referenced from the adapter:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:background="@drawable/white"
android:orientation="horizontal"
android:padding="10sp">
<ImageView
android:id="@+id/Logo"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip"
android:src="@drawable/picture1" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textStyle="bold"
android:textSize="12sp"
android:typeface="sans"
android:layout_toRightOf="@id/Logo" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="@id/name"
android:textColor="#0000CC"
android:layout_toRightOf="@id/Logo" />
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12sp"
android:layout_below="@id/description"
android:textColor="#990000"
android:layout_toRightOf="@id/Logo" />
</RelativeLayout>
Currently this code is displaying a static
image called picture1. In my database
there is a field called Unique ID
which runs from 1-60.
What I want to do is somehow display the image to match the Unique ID
- for example if the Unique ID
is 2 I want to display the image picture2.
Can anyone suggest any way I can do this ?
Thanks in advance.
You'll have to override CursorAdapter.
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view.findViewById(R.id.name)).setText(cursor.getString(cursor.getColumnIndex("name"));
... and so on for other TextView's
switch (cursor.getInt(cursor.getColumnIndex("unique_id")) {
case 0:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.image0);
break;
case 1:
((ImageView) view.findViewById(R.id.Logo)).setImageResource(R.drawable.image1);
break;
... and so on for other images
}
}
精彩评论