1

我想使用 SimpleAdapter 而不是自定义 ArrayAdapter。除了与每一行关联的图标外,一切正常。图标直接与标签相关。所以根据标签,图标可能会有所不同。

这是示例 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);

程序不会崩溃。一切都按原样显示,我什至解析“站点”,因此当单击该行时会打开一个 Web 视图。我只是无法显示图标。这甚至可以使用 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;
}
4

1 回答 1

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,所以我刚刚扩展了它的功能

于 2012-03-29T06:25:29.010 回答