androidの既製SimpleCursorAdapter
は、カーソル列のみをサポートTextViews
し、それらにマッピングするように構築されています。あなたが説明していることについては、あなたはあなた自身のアダプタオブジェクトを作る必要があります、ここで私はを使用しましたCursorAdapter
、それは舞台裏で少し仕事をしてあなたの手を汚す必要があるでしょう。これが私のサンプルの主なインスタンス化です:
cursor = datasource.fetchAllCars();
dataAdapter = new CustomCursorAdapter(this, cursor, 0);
setListAdapter(dataAdapter);
次に、ここで本格的なオブジェクト
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomCursorAdapter extends CursorAdapter {
private LayoutInflater inflater;
public CustomCursorAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
// do the layout inflation here
View v = inflater.inflate(R.layout.listitem_car, parent, false);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
// do everything else here
TextView txt = (TextView) v.findViewById(R.id.listitem_car_name);
ImageView img = (ImageView) v.findViewById(R.id.listitem_car_image);
String text = c.getString(c.getColumnIndex("COLUMN_TEXT"));
txt.setText(text);
// where the magic happens
String imgName = c.getString(c.getColumnIndex("COLUMN_IMAGE"));
int image = context.getResources().getIdentifier(imgName, "drawable", context.getPackageName());
img.setImageResource(image);
}
}
それがほとんど自明であることを願っていますが、私が「魔法が起こる場所」とラベルを付けた部分は、あなたの質問に関連する最も重要な部分であるはずです。基本的に、データベースから画像名を取得し、次の行で(通常のようにIDではなく)名前で画像を検索しようとします。次に、通常のように画像を設定します。int 0
このメソッドは、見つからない画像を返すため、エラー処理を実行する場合と実行しない場合があります。さらに、画像をロードする他の方法を使用したい場合は、それを実行する場所になります。