Reputation: 183
I'm retrieving information about places from my database. After retreiving them, I want to store them into an ArrayList (which is "ArrayList of Places", however, each time a new entry is written, it overwrites the previous entries too. I'm testing it out by printing the log. Below is the segment of codes:
response = CustomHttpClient.executeHttpPost("http://www.testing.com
/requestPlaces.php", postParameters);
JSONArray jArray = new JSONArray(response);
for(int i = 0; i < jArray.length() ; i++)
{
Places place = new Places();
JSONObject jObj = jArray.getJSONObject(i);
place.setPlace(
jObj.optString("placeID"),
jObj.optString("placeName"),
jObj.optString("placeType"),
jObj.optString("placeLat"),
jObj.optString("placeLng"),
jObj.optString("placePict"),
jObj.optString("placeRegion"));
arrPlace.add(place);
for(int j = 0; j <= i ; j++)
{
Log.e("log_tag2", arrPlace.get(j).getPlaceID());
}
This is what my log prints out that shows that the previous entry has been overwritten:
02-22 16:30:26.538: E/log_tag2(6148): p0001
02-22 16:30:26.538: E/log_tag(6148): p0002
02-22 16:30:26.538: E/log_tag2(6148): p0002
02-22 16:30:26.538: E/log_tag2(6148): p0002
02-22 16:30:26.538: E/log_tag(6148): p0003
02-22 16:30:26.538: E/log_tag2(6148): p0003
02-22 16:30:26.538: E/log_tag2(6148): p0003
02-22 16:30:26.538: E/log_tag2(6148): p0003
02-22 16:30:26.538: E/log_tag(6148): p0004
02-22 16:30:26.538: E/log_tag2(6148): p0004
02-22 16:30:26.538: E/log_tag2(6148): p0004
02-22 16:30:26.538: E/log_tag2(6148): p0004
02-22 16:30:26.538: E/log_tag2(6148): p0004
Upvotes: 0
Views: 590
Reputation: 582
may be you also want to keep the loop of logging out side the loop where you add to list.
for(int i = 0; i < jArray.length() ; i++)
{
Places place = new Places();
JSONObject jObj = jArray.getJSONObject(i);
place.setPlace(
jObj.optString("placeID"),
jObj.optString("placeName"),
jObj.optString("placeType"),
jObj.optString("placeLat"),
jObj.optString("placeLng"),
jObj.optString("placePict"),
jObj.optString("placeRegion"));
arrPlace.add(place);
}
for(int j = 0; j <= i ; j++)
{
Log.e("log_tag2", arrPlace.get(j).getPlaceID());
}
Upvotes: 0
Reputation: 43504
I would guess that you have static
fields instead of instance fields in your Places
class. Remove the static
modifier:
class Places {
static int id;
}
E.g:
class Places {
int id;
}
Upvotes: 4