2012-03-29 51 views
1

我想使用SimpleAdapter而不是自定義ArrayAdapter。除了與每行關聯的圖標之外,一切正在工作。該圖標直接與標籤相關。因此,根據標籤,圖標可能會有所不同。可以在SimpleAdapter中設置來自XML的動態圖標嗎?

下面是示例XML

<profile> 
<id>16</id> 
<name>Random Name</name> 
<site>Random URL</site> 
<icon>R.drawable.random_icon</icon> 
</profile> 

我定製行佈局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" > 

<ImageView 
    android:id="@+id/icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@+id/label" /> 

<ImageView 
    android:id="@+id/arrow" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/arrow" /> 

</LinearLayout> 

現在,這裏是我解析我的XML和建立我的適配器(編輯只是相關部分):

 NodeList children = doc.getElementsByTagName("profile"); 

    for (int i = 0; i < children.getLength(); i++) { 

      HashMap<String, String> map = new HashMap<String, String>(); 

        Element e = (Element) children.item(i); 

        map.put("id", ParseXMLMethods.getValue(e, "id")); 
     map.put("name", ParseXMLMethods.getValue(e, "name")); 
     map.put("site", ParseXMLMethods.getValue(e, "site")); 
     map.put("icon", ParseXMLMethods.getValue(e, "icon")); 
     mylist.add(map); 
    } 

      View header = getLayoutInflater().inflate(R.layout.header, null, false); 

     ListAdapter adapter = new SimpleAdapter(this, mylist, 
      R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon }); 


    final ListView lv = getListView(); 
    lv.addHeaderView(header, null, false); 
    lv.setSelector(R.color.list_selector); 
    setListAdapter(adapter); 

該程序不會崩潰。一切似乎都是這樣,我甚至解析「網站」,所以當點擊行時打開了網頁視圖。我只是無法顯示圖標。這甚至可以用SimpleAdapter嗎?

更新:這裏是重寫getView()方法:

public View getView(int position, View convertView, ViewGroup parent) { 
    View view = super.getView(position, convertView, parent); 

    int idImage = context.getResources().getIdentifier("icon","drawable", context.getPackageName()); 

    // ???????? 

    return view; 
} 
+1

我不認爲它會這樣工作。您將需要製作一個自定義適配器,並在其中將圖像分配給imageview – Akhil 2012-03-29 05:49:45

回答

0

我可以推薦你實現這一目標是通過在擴展SimpleAdapter使用則getIdentifier方法從資源。但是,首先你需要改變你的XML一點:

而是保存圖像的可編程參考到你的XML,你應該簡單地保存文件名:

<profile> 
    <id>16</id> 
    <name>Random Name</name> 
    <site>Random URL</site> 
    <icon>random_icon</icon> 
</profile> 

現在,您SimpleAdapter延伸到您自己的適配器類並覆蓋getView方法。在這種方法中,你可以從名稱重新構建資源ID,並將其設置爲您的ImageView:

int idImage = context.getResources().getIdentifier("nameOfResource", "drawable", context.getPackageName()); 

更新

public class MyAdapter extends SimpleAdapter { 

    private Context context; 

    List<HashMap<String, String>> lstData = new ArrayList<HashMap<String,String>>(); 

    public MyAdapter(Context context, List<HashMap<String, String>> items) { 
     super(context, items, android.R.id.text1, R.layout.rowlayout, new String[] { "name", "icon" }, new int[] { R.id.label, R.id.icon }); 

     this.context = context; 
     lstData = items; 
    } 




    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ViewHolder holder = null; 

     //smart initialization  
     if(convertView == null){ 
      LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflator.inflate(R.layout.rowlayout, parent, false); 

      holder = new ViewHolder(); 
      holder.title = (TextView) convertView.findViewById(R.id.label); 
      holder.img = (ImageView) convertView.findViewById(R.id.icon); 
      convertView.setTag(holder); 
     } 
     else 
      holder = (ViewHolder) convertView.getTag(); 

     //get item 
     HashMap<String, String> map = lstData.get(position); 

     //setting title 
     holder.title.setText(map.get("name")); 

     int idImage = context.getResources().getIdentifier(map.get("icon"),"drawable", context.getPackageName()); 
     holder.img.setImageResource(idImage); 

     return convertView; 
    } 



    //this is better approach as suggested by Google-IO for ListView 
    private static class ViewHolder{ 
     TextView title; 
     ImageView img; 
    } 

} 

* 這個類的代碼是給你一個參考看看你的適配器應該如何。更合適的方式是使用ArrayAdapter,但由於您已經使用SimpleAdapter,所以我只是擴展了它的功能

+0

感謝您的澄清。聽起來像我需要找到SimpleAdapter和製作我自己的ArrayAdapter之間的中間地帶。你能給出一個這樣的語句的代碼示例「將你的SimpleAdapter擴展到你自己的適配器類」嗎?我熟悉擴展一個類,但是如何擴展某個特定的SimpleAdapter? – KickingLettuce 2012-03-29 15:45:51

+0

我相信我在Google的幫助下回答了我自己的問題:http://eureka.ykyuen.info/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview -with-simpleadapter/- 我希望這可以工作? – KickingLettuce 2012-03-29 16:15:06

+0

是的,多數民衆贊成什麼:)好運 – waqaslam 2012-03-29 16:42:41

相關問題