gpt4 book ai didi

java - 将数据填充到 Listview 但 Arrayadapter 不工作

转载 作者:行者123 更新时间:2023-11-29 22:46:23 25 4
gpt4 key购买 nike

我正在尝试从我的 arrayList 中填充数据 ListView 。但出于某种原因,我收到错误 error: no suitable constructor found for ArrayAdapter(activity2.SendPostRequest,int,int,ArrayList<HashMap<String,String>>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int,HashMap<String,String>[]) is not applicable

                JSONArray json = new JSONArray(data);
ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

HashMap<String, String> map = new HashMap<String, String>();

try {
for(int i=0;i<json.length();i++){
JSONObject e =json.getJSONObject(i);

map.put("id", String.valueOf(i));
map.put("Name", "Vorname: " + e.getString("meta_value"));
map.put("orderid", "id: " + e.getString("post_id"));
arrayList.add(map);
return map.toString();
}
ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String, String>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

list.setAdapter(adapter);
}catch (JSONException e){
Log.e(this.getClass().getName(), "Some JSON error occurred" + e.getMessage());

}

到处都找不到解决方案

最佳答案

ArrayAdapter(activity2.SendPostRequest,int,int,ArrayList>)

Problem is you pass this as the first parameter for giving context to the adapter.

constructor ArrayAdapter.ArrayAdapter(Context,int,int,HashMap<String,String>[])

Solution

代替这个pass this

getContext() or getActivity()

因为这可能指向不同的上下文,具体取决于您使用的是什么。

引用这个link深入了解 this关键字

关于java - 将数据填充到 Listview 但 Arrayadapter 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58364831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com