【问题标题】:ListView from XML with custom ImageView来自 XML 的 ListView 与自定义 ImageView
【发布时间】:2014-04-10 20:03:28
【问题描述】:

我正在从 XML 填充布局。我不太了解本教程,所以我只能填充 TextViews。如何为每个列表项的 ImageView 动态分配图像资源?

我目前在做什么:

    ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromResource(getApplicationContext(),
            R.raw.symbols);
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SYMBOL);
    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key => value
        map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
        map.put(KEY_FILENAME, parser.getValue(e, KEY_FILENAME));
        map.put(KEY_ABBR, parser.getValue(e, KEY_ABBR));
        map.put(KEY_ELEMENT, parser.getValue(e, KEY_ELEMENT));

        // adding HashList to ArrayList
        menuItems.add(map);
    }

    // Adding menuItems to ListView
    ListAdapter adapter = new SimpleAdapter(this, menuItems,
            R.layout.symbol_list_item, new String[] { KEY_NAME,
                    KEY_FILENAME, KEY_ABBR, KEY_ELEMENT }, new int[] {
                    R.id.name, R.id.filename, R.id.abbr, R.id.element });

    setListAdapter(adapter);

如果重要的话,我不是简单地为 ImageView 设置可绘制对象。我正在使用来自 androidSVG 库的自定义 SVGImageView。我为动态拉取 SVG 文件所做的不同活动是这样的:

symbolImageView = (SVGImageView) findViewById(R.id.symbolImage);
symbolImageView.setImageResource(this.getResources().getIdentifier(symbol.getFilename(), "raw", this.getPackageName()));

这就是我的占位符 ImageViews 目前的样子:

【问题讨论】:

  • 您的视图集将通过您的适配器处理。
  • 您需要创建一个自定义适配器,一个可能扩展 BaseAdapter 的类。在该类的 getView() 方法中,您需要确定文本字符串,然后根据该方法设置背景图像。
  • 这是一个很好的简单列表示例适配器stackoverflow.com/questions/8166497/…
  • 使用自定义适配器是我需要做的。不过,我无法将您的评论设置为答案。

标签: android xml listview android-listview


【解决方案1】:

只是为了得到答案,您的问题在于缺少一个适配器来确定列表中的视图并动态设置该视图的背景。 Android 提供的 Simple Adapter 并没有真正超越基础。它需要一个列表并在屏幕上直观地表示它,它没有很大的灵活性。但是,您可以创建一个扩展适配器的类,如 BaseAdapter,然后扩展其基本功能。这主要在 getView() 中完成。基本布局类似于:

public class ListAdapter extends BaseAdapter {
    Context context;
    int resource;
    List<Item> items = new List<Item>();

public ListAdapter(Context context, int resource, List<Item> items) {
    super(context, resource, items);
    this.context = context;
    this.resource = resource;
    this.items = items;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(resource, parent, false);
    }

    // Dynamically work with each view in the list
}

可以在此堆栈答案Custom Adapter for List View 中看到更复杂的示例

【讨论】:

    【解决方案2】:

    尝试使用setBackgroundResource 而不是setImageResource

    symbolImageView.setBackgroundResource(this.getResources().getIdentifier(symbol.getFilename(), "raw", this.getPackageName()));
    

    【讨论】:

    • 这段代码去哪儿了?这就是我感到困惑的部分。
    • 你正在使用setImageResource,尝试使用setBackgroundResource
    • 我觉得不是这个问题,我觉得问题是他需要能够根据列表中的其他值动态设置背景
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多