For example if i have to create custom list view about type of food, in each list_item consist of food type logo, food type name. If i store food type name in string resource and store picture of each food type in drawable.
1) How can i get the picture from drawable and set to List view? 2) How to get the picture that match name of food type
Thanks you in advance.
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;
}
}
}
Just like you did by setting the text, but for a ImageView.
ImageView foodPhoto = (ImageView)v.findViewById(R.id.food_photo);
foodPhoto.setImageDrawable(foodDrawable);
You just need to create a list of drawable ids to know which drawable to use.