gpt4 book ai didi

java - 填充Listview时Arraylist和Hashmap的问题

转载 作者:行者123 更新时间:2023-12-01 23:01:54 25 4
gpt4 key购买 nike

我正在尝试将数据从 JSON 文件获取到 HashMap,然后获取到我的 Arraylist,它应该显示在 Listview 中。但它没有显示任何东西。你能帮我吗?

公共(public)类 SendPostRequest 扩展 AsyncTask {

    String data ="";
protected void onPreExecute(){}

protected String doInBackground(String... arg0) {

try{


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 arrayAdapter = new ArrayAdapter(activity2.this, android.R.layout.simple_list_item_1,arrayList);

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

}
return data;


}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}

}

最佳答案

要填充 ListView ,您需要在创建数组适配器后添加这段代码

ListView myListView = (ListView) findViewById(R.id.idOfYourListView)

runOnUiThread(new Runnable()
{
@Override
public void run()
{
myListView.setAdapter( arrayAdapter );
}
});

关于java - 填充Listview时Arraylist和Hashmap的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58417451/

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