Reputation: 1111
I just cant see what is wrong with my code even after following all the other answers here. Can someone please look at my code and tell me why the list view only displays the last item in the loop Here is my code
@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);
}
This is what i get when i print the arrayList in console enter image description here
Upvotes: 0
Views: 51
Reputation: 74
Print all your List elements in console! If all of the elements were same then you have to take care of the String object that you're adding to your list, it may be because of all elements in list may point to a single object, you can avoid it by moving the "String fullappointment;" inside the for loop. If error continues then post your adapter class!
Upvotes: 1