在列表视图中多次仅从数据库获取最后一个值

时间:2019-04-17 10:43:13

标签: java android android-volley

am目前正在使用凌空抽空,并且正在尝试从我的php接收foreach结果并将其发送到我的列表视图。问题是,我不断收到最后一个值4次,因为在数据库中只有两个值。还只显示了这两个值中的最后一个值,并且显示了4次

我尝试使用jsonObject和jsonArray,但我不知道自己在做什么错

private void loadList() {
    displayLoader();
    JSONObject request = new JSONObject();
    try {
        //Populate the request parameters

        request.put(KEY_USER, userID);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    JsonObjectRequest jsArrayRequest = new JsonObjectRequest
            (Request.Method.POST, login_url, request, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    pDialog.dismiss();

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

                            Log.e ( "response", "" + response);
                            //JSONObject obj = response.getJSONObject(i);
                            ModelTestData test = new ModelTestData(response.getString("research")
                                    ,response.getString("name")
                                    ,response.getString("hospital")
                                    ,response.getString("type"));

                            test.setResearch(response.getString("research"));
                            test.setName(response.getString("name"));
                            test.setHospital(response.getString("hospital"));
                            test.setType(response.getString("type"));


                            // adding movie to movies array
                            testList.add(test);


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    cAdapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    pDialog.dismiss();

                    //Display error message whenever an error occurs
                    Toast.makeText(getApplicationContext(),
                            "Olau", Toast.LENGTH_SHORT).show();
                    Toast.makeText(getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();

                    //  Toast.makeText(getApplicationContext(),
                    //      "Finally success second", Toast.LENGTH_SHORT).show();

                }
            });

    // Access the RequestQueue through your singleton class.
    MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
}

这就是响应的样子

{"research":"I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.","name":"Pestal feel","hospital":"Saint Paule","type":"Pancrease","0":true}{"research":"I got this error while debugging an android app on Android Studio via USB. Connection attempts failed with one tablet but not with my regular test tablet. It turned out that the failure was due to the fact that I hadn't enabled WIFI settings on the failing machine. So I enabled WIFI on it and it connected to server OK.","name":"Coner stone","hospital":"Saint Paule","type":"Breast","0":true,"1":true}

0 个答案:

没有答案