2012-03-29 43 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

相关问题