如何在ListView android中显示图像

如何在ListView android中显示图像,android,listview,imageview,hashmap,simpleadapter,Android,Listview,Imageview,Hashmap,Simpleadapter,我一直在四处寻找如何做到这一点的例子。在StackOverflow上使用SimpleAdapter和HashMaps的方法。 这是我的密码。它不起作用了。我所能看到的只是一个列表的空行 listItems = new ArrayList<HashMap<String,Object>>(); HashMap<String,Object> listData1 = new HashMap<String,Object>(); HashMap<Str

我一直在四处寻找如何做到这一点的例子。在StackOverflow上使用SimpleAdapter和HashMaps的方法。 这是我的密码。它不起作用了。我所能看到的只是一个列表的空行

listItems = new ArrayList<HashMap<String,Object>>();

HashMap<String,Object> listData1 = new HashMap<String,Object>();
HashMap<String,Object> listData2 = new HashMap<String,Object>();

image_1 = getResources().getDrawable(R.drawable.camera_icon_focus_dim);
image_2 = getResources().getDrawable(R.drawable.camera_icon_scene_mode);

listData1.put("icon focus", image_1);
listData2.put("icon_scene", image_2);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems,
  R.layout.image_list_item, new String[]{"icon_focus"}, 
  new int[]{R.id.list_view_item1});

optionsList.setAdapter(listItemAdapter);
listItems=newArrayList();
HashMap listData1=新HashMap();
HashMap listData2=新HashMap();
image\u 1=getResources().getDrawable(R.drawable.camera\u图标\u焦点\u暗显);
image_2=getResources().getDrawable(R.drawable.camera_图标\u场景\u模式);
listData1.put(“图标焦点”,图像_1);
listData2.put(“图标\场景”,图像\ 2);
添加(listData1);
添加(listData2);
SimpleAdapter listItemAdapter=新SimpleAdapter(此,listItems,
R.layout.image_列表_项,新字符串[]{“icon_focus”},
新int[]{R.id.list_view_item1});
optionsList.setAdapter(listItemAdapter);
image\u list\u item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView   xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="12dip"       
        android:layout_width="wrap_content"       
        android:layout_height="wrap_content"        
        android:id="@+id/listitem_img"
    />
<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view_item1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
/>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView   xmlns:android="http://schemas.android.com/apk/res/android"
        android:paddingTop="12dip"       
        android:layout_width="wrap_content"       
        android:layout_height="wrap_content"        
        android:id="@+id/listitem_img"
    />
<?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view_item1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1.0"
/>


请告诉我我做错了什么。

您的
HashMap
中的字段需要匹配。试试这个:

listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "icon_id";

HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();

listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(
    this,
    listItems,
    R.layout.image_list_item,
    new String[] { fieldName },
    new int[] { R.id.listitem_img });
listItems=newArrayList();
String fieldName=“icon\u id”;
HashMap listData1=新HashMap();
HashMap listData2=新HashMap();
listData1.put(字段名,R.drawable.camera\u图标\u焦点\u暗显);
listData2.put(字段名,R.drawable.camera\u图标\u场景\u模式);
添加(listData1);
添加(listData2);
SimpleAdapter listItemAdapter=新SimpleAdapter(
这
清单项目,
R.layout.image\u列表\u项,
新字符串[]{fieldName},
新的int[]{R.id.listitem_img});

您的
HashMap
中的字段需要匹配。试试这个:

listItems = new ArrayList<HashMap<String,Integer>>();
String fieldName = "icon_id";

HashMap<String, Integer> listData1 = new HashMap<String, Integer>();
HashMap<String, Integer> listData2 = new HashMap<String, Integer>();

listData1.put(fieldName, R.drawable.camera_icon_focus_dim);
listData2.put(fieldName, R.drawable.camera_icon_scene_mode);

listItems.add(listData1);
listItems.add(listData2);

SimpleAdapter listItemAdapter = new SimpleAdapter(
    this,
    listItems,
    R.layout.image_list_item,
    new String[] { fieldName },
    new int[] { R.id.listitem_img });
listItems=newArrayList();
String fieldName=“icon\u id”;
HashMap listData1=新HashMap();
HashMap listData2=新HashMap();
listData1.put(字段名,R.drawable.camera\u图标\u焦点\u暗显);
listData2.put(字段名,R.drawable.camera\u图标\u场景\u模式);
添加(listData1);
添加(listData2);
SimpleAdapter listItemAdapter=新SimpleAdapter(
这
清单项目,
R.layout.image\u列表\u项,
新字符串[]{fieldName},
新的int[]{R.id.listitem_img});

这可能会对你有所帮助:谢谢你,本约尔。这正是我想说的。这可能会对你有所帮助:谢谢你,本约。事实上,这就是我想说的,成功了!谢谢:)但是,我不明白为什么。您能解释一下吗?有两个并行数组传递给
simpledapter
,一个带有
String
,另一个带有
int
。每个位置将
HashMap
中的键映射到模板中指定的
视图(
R.layout.image\u list\u项
)。ListView从列表中获取一个项目,它是一个
HashMap
,使用键(
“icon\u id”
,在示例中)获取值以填充
视图(
R.id.listitem\u img
)。通过使用不同的键存储列表项模板,可以在列表项模板中显示多个字段,将该键附加到
字符串[]
中,并将相应的视图id附加到
int[]
中,为什么文本没有添加到列表视图中@chiukiIt成功了!谢谢:)但是,我不明白为什么。您能解释一下吗?有两个并行数组传递给
simpledapter
,一个带有
String
,另一个带有
int
。每个位置将
HashMap
中的键映射到模板中指定的
视图(
R.layout.image\u list\u项
)。ListView从列表中获取一个项目,它是一个
HashMap
,使用键(
“icon\u id”
,在示例中)获取值以填充
视图(
R.id.listitem\u img
)。通过使用不同的键存储列表项模板,可以在列表项模板中显示多个字段,将该键附加到
字符串[]
中,并将相应的视图id附加到
int[]
中,为什么文本没有添加到列表视图中@丘基