Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使用drawable中的图像自定义每个列表项视图?_Android_Imageview_Android Listview_Android Arrayadapter_Custom Lists - Fatal编程技术网

Android 如何使用drawable中的图像自定义每个列表项视图?

Android 如何使用drawable中的图像自定义每个列表项视图?,android,imageview,android-listview,android-arrayadapter,custom-lists,Android,Imageview,Android Listview,Android Arrayadapter,Custom Lists,例如,如果我必须创建关于食品类型的自定义列表视图,则每个列表中的项目由食品类型徽标、食品类型名称组成。如果我将食物类型名称存储在字符串资源中,并将每种食物类型的图片存储在drawable中 1) 如何从drawable获取图片并设置为列表视图? 2) 如何获取与食物类型名称匹配的图片 提前谢谢你 public class FoodListActivity extends ListActivity { private CustomListAdapter listAdapter;

例如,如果我必须创建关于食品类型的自定义列表视图,则每个列表中的项目由食品类型徽标、食品类型名称组成。如果我将食物类型名称存储在字符串资源中,并将每种食物类型的图片存储在drawable中

1) 如何从drawable获取图片并设置为列表视图? 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;


    }

  }

}
公共类FoodListActivity扩展了ListActivity{
私有CustomListAdapter-listAdapter;
私有字符串[]食品类型名称;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.foodlistholder);
食物类型名称=getResources().getStringArray(R.array.food名称数组);
listAdapter=新的CustomListAdapter(这个,R.layout.list\u项目\u食物,食物类型\u名称);
setListAdapter(listAdapter);
私有类CustomListAdapter扩展了ArrayAdapter{
私有字符串[]项;
公共CustomListAdapter(上下文,int textViewResourceId,
字符串[]项){
super(上下文、textViewResourceId、项);
这个项目=项目;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.列表\项目\食品,空);
}
TextView foodNameStr=(TextView)v.findViewById(R.id.food\u name\u txt);
foodNameStr.setText(项目[位置]);
//如何设置与食品类型名称匹配的图像视图窗体drawable
返回v;
}
}
}

就像您通过设置文本所做的那样,但是对于ImageView

ImageView foodPhoto = (ImageView)v.findViewById(R.id.food_photo);
foodPhoto.setImageDrawable(foodDrawable);

您只需要创建一个可绘制ID列表,就可以知道要使用哪个可绘制ID。

最好浏览一下这个示例,谢谢您的回答,不过,我认为会有更漂亮的解决方案。在这个链接之后,它只有2个图像(R.drawable.no和R.drawable.ok)它使用if/ese条件切换2个图像。如果,我有很多类型的食物可能是40个列表项。我需要写一个条件来切换所有40个项目吗?如果是这样,我将做一个切换情况。无论如何,谢谢你