現在、次のように Android アプリケーションで PNG 画像を描画しています。
ImageView image = new ImageView(context);
image.setImageDrawable(context.getResources().getDrawable(R.drawable.testimage))
データベースに画像名のリストがある場合、画像名を使用して上記のドローアブルを設定する方法はありますか? データベースを通過するコードは既にあります。ここから取得した値に基づいて画像を描画しようとしています。
たとえば、DB のレコード:
ID: Name: ImageName:
- Test testimage
したがって、このレコードを読んでいるとき、「testimage」という値の文字列があり、画像ドローアブルを に設定したいと思いR.drawable.testimage
ます。
私がそれを行うことを考えていた1つの方法は、次のようなものになるでしょう:
int image = R.drawable.blank; // blank image
// testimage.png is the image name from the database
if(imageName.toString().equals("testimage.png"))
image = R.drawable.testimage;
else if(imageName.toString().equals("another.png"))
image = R.drawable.another;
else if(imageName.toString().equals("etc.png"))
image = R.drawable.etc;
ただし、これはあまり効率的ではありません。
ありがとう