我的jsonarray没有出现在我的arraylist中

时间:2017-02-02 21:08:02

标签: java android json list android-volley

我有jsonArray,但它没有显示在我的列表中。我已经添加了所有需要的xml文件。

我的截击是有效的,我对jsonarray有回应,但我的问题是它没有显示在列表中。

// json array response url
private String urlJsonArry = "http://api.androidhive.info/volley/person_array.json";

private static String TAG = MainActivity.class.getSimpleName();


// Progress dialog
public ProgressDialog pDialog;
public AppController app ;


// temporary string to show the parsed response
private String jsonResponse;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.array_list);
    ListView listView = (ListView) findViewById(R.id.list);
    items = new ArrayList<String>();
    adapter = new ArrayAdapter(this, R.layout.list_item, R.id.txt, (List) items);
    listView.setAdapter(adapter);
    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Please wait...");
    pDialog.setCancelable(false);

    makeJsonArrayRequest();
}

private void makeJsonArrayRequest() {

    showpDialog();

    JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    try {

                        for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response
                                        .get(i);
                                items.add(person.getString("name"));
                            Toast.makeText(getApplicationContext(),
                                    person.getString("name"),
                                    Toast.LENGTH_LONG).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            hidepDialog();
        }
    });

    // Adding request to request queue
    app.getInstance().addToRequestQueue(req);
}

private void showpDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hidepDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

那么请你能帮助我在我的arraylist中展示我的jsonarray吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您需要在JSON解析循环后调用adapter.notifyDataSetChanged()

                     for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response
                                        .get(i);
                                items.add(person.getString("name"));
                            Toast.makeText(getApplicationContext(),
                                    person.getString("name"),
                                    Toast.LENGTH_LONG).show();
                        }
                       adapter.notifyDataSetChanged();