【问题标题】:TreeMap <Key,List<String>> to List viewTreeMap <Key,List<String>> 到列表视图
【发布时间】:2013-12-12 09:16:15
【问题描述】:

我有一个带有键值对的 TreeMap,键是一个表示某种类型的字符串。基于此,我可以添加四个其他类型的属性作为列表。现在对于 的每个条目,我想在列表视图中填充一行。我应该制作什么类型的适配器。另外我必须注意我必须重写 getView() 方法,因为我想根据键值显示不同的图片。任何提示或教程?

【问题讨论】:

  • 你用的是TreeMap还是hashMap??
  • 一个 TreeMap,用于保持我的键排序。
  • 看看this

标签: android list android-listview treemap


【解决方案1】:

您可以使用简单的基本适配器。在该适配器中,在获取视图方法中,您可以使用另一个列表适配器来扩展该列表

【讨论】:

  • 您能否提供更多关于如何扩充列表部分的提示?
  • 可以使用自定义列表适配器getview方法
  • 是的,我知道,我在问题本身中描述了这一点。但是如何将列表项分配给一行?然后重复?
  • 我刚刚得到了一个神圣的愿景,我可以将整个树图转换成一个列表!保留排序顺序!
  • 如果我没弄错的话,你想要二级列表视图之类的东西
【解决方案2】:

您可以轻松摆脱使用BaseAdapter。这里的技巧是从指定索引处的映射中获取键值对。这并不难——而且随着您的地图被排序,您每次都会得到相同的顺序。你会有这样的东西:

public class MapAdapter<K, V> extends BaseAdapter {
    Context context;
    Map<K, V> data;

    public MapAdapter(Context _context, Map<K, V> _data) {
        context = _context;
        data = _data;
    }

    public int getCount() { return data.size(); }

    public Object getItem(int position) {
        K key = map.keySet().toArray()[position];
        V value = map.get(key);
        return AbstractMap.SimpleEntry(key, value);
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        AbstractMap.SimpleEntry<K, V> entry = (AbstractMap.SimpleEntry<K, V>)getItem(position);
        K key = entry.getKey();
        V value = entry.getValue();

        MyRowView rowView = (MyRowView)convertView;
        if(rowView == null) {
            rowView = ...    //create your view by inflating or otherwise
        }

        //Now you have the view and details of your key and value - populate the row
        ...

        return rowView;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 2012-03-23
    • 2018-07-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多