我试图在列表视图中设置一个arrayList,只有最后一项出现在列表视图中

时间:2017-04-14 01:58:10

标签: java android

即使遵循了所有其他答案,我也无法看到我的代码出了什么问题。有人可以看看我的代码并告诉我为什么列表视图只显示循环中的最后一项 这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_appointments);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);
    db = new SQLiteHandler(getApplicationContext());
    listview = (ListView)findViewById(R.id.appointmentList);
    //  cancel request tag
    String tag_string_req = "req_appointments";

    pDialog.setMessage("fetching appointments...");
    showDialog();

    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_FETCH_APPOINTMENTS, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Appointment fetch response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    JSONArray appointments = jObj.getJSONArray("appointments");
                    //Log.d(TAG, String.valueOf(appointments.length()));
                    int count=appointments.length();
                    String fullAppointment="";
                    for (int i=0;i<count;i++){
                        JSONObject jsonObj2 = appointments.getJSONObject(i);
                        String date = jsonObj2.getString("date");
                        String status=jsonObj2.getString("status");
                        String timestamp=jsonObj2.getString("timestamp");
                        String appointmentStatus="";
                        if (status.matches("PENDING")){
                            appointmentStatus="This appointment is still pending and has not been confirmed by the doctor";
                        }else{
                            appointmentStatus="This appointment has already been confirmed by the doctor";
                        }
                        //Log.d(TAG,date+":"+status+":"+timestamp);
                        fullAppointment="Date of consultation: "+date +"\n"+ appointmentStatus +"\n"+ "Booked on date :"+timestamp;
                        //myList.add(fullAppointment);
                        adapter.add(fullAppointment);
                        adapter.notifyDataSetChanged();

                    }


                } else {

                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(
                            MyAppointments.this,
                            HomeActivity.class);
                    startActivity(intent);
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Appointment Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            HashMap<String, String> user = db.getUserDetails();
            String userID = user.get("uid");
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("userID", userID);

            return params;
        }

    };

    // Adding request to request queue
    myConnection.getInstance().addToRequestQueue(strReq, tag_string_req);

    adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,0);
    listview.setAdapter(adapter);

}

这是我在控制台中打印arrayList时得到的 enter image description here

1 个答案:

答案 0 :(得分:1)

在控制台中打印所有List元素!如果所有元素都相同,那么你必须处理你要添加到列表中的String对象,这可能是因为列表中的所有元素都可能指向一个对象,你可以通过移动来避免它&#34; String fullappointment;&#34;在for循环中。如果错误仍然存​​在,请发布适配器类!