有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java my jsonarray不显示在我的arraylist中

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

我的截击很有效,我对jsonarray有回应,但我的问题是它没有出现在列表中

// json array response url
private String urlJsonArry = "http://api.安卓hive.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) 个答案

  1. # 1 楼答案

    您需要在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();