【问题标题】:Cannot Populate listView with ArrayList<HashMap<String,?>>>无法使用 ArrayList<HashMap<String,?>>> 填充 listView
【发布时间】:2014-03-07 07:17:14
【问题描述】:

ArrayList<HashMap<String, ?>>  list;
f(!list.isEmpty()){

          adapter = new MySimpleArrayAdapter(getActivity(),R.layout.inner_base_header_cutom, list);
          listview.setAdapter(adapter);
          Log.i("LIST Active", "LIST Active" + list.size());
      }

public class MySimpleArrayAdapter extends ArrayAdapter<ArrayList<HashMap<String, ?>>> {
          private final Context context;
          private final ArrayList<HashMap<String, ?>> values;

          public MySimpleArrayAdapter(Context context, int innerBaseHeaderCutom, ArrayList<HashMap<String, ?>> list) {
            super(context, innerBaseHeaderCutom, list);
            this.context = context;
            this.values = list;
          }

}


错误:

The constructor ArrayAdapter<ArrayList<HashMap<String,?>>>(Context, int, ArrayList<HashMap<String,?>>) is undefined

也无法移除 ListView:

remove(list.get(position));

错误:

The method remove(ArrayList<HashMap<String,?>>) in the type ArrayAdapter<ArrayList<HashMap<String,?>>> is not applicable for the arguments (HashMap<String,capture#2-of ?>)

【问题讨论】:

  • 代码中的列表是什么?
  • ` ArrayList>`,我在上面添加了一些。
  • ArrayList&lt;HashMap&lt;String, ?&gt;&gt; list 为什么是?。它们的值不是字符串吗?还有你在哪里填充列表
  • 也许尝试使用List 而不是ArrayList
  • @lordzden 检查我的答案...

标签: java android android-listview arraylist


【解决方案1】:

调用超类的构造函数时去掉最后一个参数

super(context, innerBaseHeaderCutom);

编辑:
另一个你可以尝试的想法是改变这个:

public class MySimpleArrayAdapter extends ArrayAdapter<ArrayList<HashMap<String, ?>>> {

public class MySimpleArrayAdapter extends ArrayAdapter<HashMap<String, ?>> {

并在构造函数中做同样的改变。

【讨论】:

  • 适配器如何知道应该填充许多列表,先生。仍然没有填充我检查了list.size,我得到了三个。
  • 因为 ArrayAdpater 希望您传递您将在 Array 中拥有的对象类型,而不是保存这些对象的结构(即 ArrayList)。
【解决方案2】:
You are creating ArrayList with ArrayList<HashMap<String, ?>> instead you can try ArrayList<HashMap<String, ? extends Object>>

检查示例代码和输出。

public class MyJavaTesting{
public MyJavaTesting(ArrayList<HashMap<String, ? extends Object>> list) {
    System.out.println(list);
}

public static void main(String[] args) {
    ArrayList<HashMap<String, ? extends Object>> list = new ArrayList<HashMap<String,?>>();
    HashMap<String, String> map1 = new HashMap<String, String>();
    map1.put("Key", "value");
    list.add(map1);

    HashMap<String, Object> map2 = new HashMap<String, Object>();
    map2.put("Key", new Object());
    list.add(map2);

    MyJavaTesting manager = new MyJavaTesting(list);
}
}

输出: [{Key=value}, {Key=java.lang.Object@4f1d0d}]

【讨论】:

    猜你喜欢
    • 2011-12-31
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2014-07-23
    • 2013-06-01
    相关资源
    最近更新 更多