【问题标题】:Can set dynamic icon from XML in SimpleAdapter?可以在 SimpleAdapter 中从 XML 设置动态图标吗?
【发布时间】:2012-03-29 05:40:24
【问题描述】:

我想使用 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;
}

【问题讨论】:

  • 我不认为它会那样工作。您需要制作一个自定义适配器,并在其中将您的图像分配给 imageview

标签: android xml image simpleadapter


【解决方案1】:

我可以建议您通过使用扩展 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,所以我只是扩展了它的功能

【讨论】:

  • 感谢您的澄清。听起来我需要在 SimpleAdapter 和制作我自己的 ArrayAdapter 之间找到一个中间立场。你能给出这个语句“将你的 SimpleAdapter 扩展到你自己的适配器类”的编码示例吗?我对扩展一个类很熟悉,但是你如何扩展像 SimpleAdapter 这样特定的东西?
  • 我相信我在 Google 的帮助下回答了我自己的问题:eureka.ykyuen.info/2010/03/15/… -- 我希望这可行吗?
  • 好的,我知道这是一个基本问题......但是我该如何使用“idImage”?此外,“nameOfResource”指的是...?图形文件?动态的 XML 图标名称? (我在原始问题中添加了新的 getView() 方法)。
  • @Wasqas 谢谢!这正如我所愿。现在我明白为什么只推荐使用简单的 ArrayAdapter 了。我认为这只是一个简单的覆盖——而不是重新做和膨胀整个布局。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 2011-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多