たとえば、食品の種類に関するカスタム リスト ビューを作成する必要がある場合、各 list_item は食品の種類のロゴ、食品の種類の名前で構成されます。食品タイプ名を文字列リソースに保存し、各食品タイプの写真をドローアブルに保存するとします。
1)ドローアブルから画像を取得してリストビューに設定するにはどうすればよいですか? 2) 食べ物の種類の名前と一致する写真を取得する方法
よろしくお願いします。
public class FoodListActivity extends ListActivity {
private CustomListAdapter listAdapter;
private String[] food_type_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foodlistholder);
food_type_name = getResources().getStringArray(R.array.food_name_array);
listAdapter = new CustomListAdapter(this, R.layout.list_item_food, food_type_name);
setListAdapter(listAdapter);
private class CustomListAdapter extends ArrayAdapter<String>{
private String[] items;
public CustomListAdapter(Context context, int textViewResourceId,
String[] items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_item_food, null);
}
TextView foodNameStr = (TextView)v.findViewById(R.id.food_name_txt);
foodNameStr.setText(items[position]);
//How to set Image view form drawable, which match the food's type name
return v;
}
}
}