2011-12-07 20 views
0

私はこれを行う方法の例を探しています。 StackOverflowでここでSimpleAdapterとHashMapsを使用していたアプローチを見てみましょう。 これは私のコードです。動いていない。私が見ることができるのは、リストの空の行です。ListViewで画像を表示する方法android

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); 

image_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" 
    /> 

list_item.xml:

<?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" 
/> 

私が間違ってやっているものを私に教えてください。

+1

:これを試してみてください http://stackoverflow.com/a/8267263/265167 –

+1

([これはまた、あなたを助ける] http://stackoverflow.com/a/3068012/593709 ) –

+0

Benjolありがとうございました。それは私が実際に言いたかったことです。 – Namratha

答えて

4

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 }); 
+0

それは働いた!ありがとうございました:)しかし、私は理由を理解できませんでした。説明していただけますか? – Namratha

+0

'SimpleAdapter'に渡される2つの並列配列があります。一つは' String'、もう一つは 'int'です。それぞれの位置は 'HashMap'のキーをテンプレートで指定された' View'(あなたの場合は 'R.layout.image_list_item')にマップします。 ListViewは 'HashMap'であるリストからアイテムを取り出し、' View'(例では 'R.id.listitem_img')に値を取得するためにキー(この例では' 'icon_id" ')を使います)。リストアイテムテンプレートに複数のフィールドを表示するには、別のキーでそれを保存し、そのキーを 'String []'に追加し、対応するビューIDを 'int []'に追加します。 – chiuki

+0

なぜテキストがリストビュー? @chiuki – ajdeguzman

関連する問題