私ListViewは 2 つの を持っていTextViewsます。背景画像を のいずれかに動的に設定しようとしていますTextViews。各項目/行のカテゴリに応じて、表示したい約 18 の異なる画像があります。画像の名前"abc1"は"abc2"、 などです。カスタムのコードは次のCursorAdapterとおりです。
private class MyListAdapter extends SimpleCursorAdapter {
public MyListAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
super(context, layout , cursor, from, to);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Get the resource name and id
String resourceName = "R.drawable.abc" + cursor.getString(7);
int resid = context.getResources().getIdentifier(resourceName,"drawable",context.getPackageName());
// Create the idno textview with background image
TextView idno = (TextView) view.findViewById(R.id.idno);
idno.setText(cursor.getString(3));
idno.setBackgroundResource(resid);
// create the material textview
TextView materials = (TextView) view.findViewById(R.id.materials);
materials.setText(cursor.getString(1));
}
}
デバッグで実行すると、リソースが見つからなかったことを示すresid常に返されます。ID: は正しく見え0ます。イメージ ファイルをフォルダにインポートしたところ、にリストされています。resourceName"R.drawable.abc1"res/drawableR.java
これは正しい方法ですか、それともより良い解決策がありますか?