填充Listview时Arraylist和Hashmap出现问题

时间:2019-10-16 16:03:02

标签: java android arrays json listview

我正在尝试将数据从我的JSON文件获取到HashMap,然后再获取到Arraylist,它应该显示在Listview中。但是它什么也没显示。您能帮我吗?

公共类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());
        }

    }

2 个答案:

答案 0 :(得分:0)

要填充列表视图,您需要在创建阵列适配器之后添加这段代码

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

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

答案 1 :(得分:0)

您应该立即将Reviewer替换为ListView