我正在从我的数据库中检索有关地点的信息。在检索它们之后,我想将它们存储到一个ArrayList(它是“ArrayList of Places”),但是,每次写入一个新条目时,它都会覆盖以前的条目。我正在通过打印日志来测试它。是代码段:
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());
}
这是我的日志打印出来的,表明上一个条目已被覆盖:
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
答案 0 :(得分:4)
我猜你的static
课程中有Places
个字段而不是实例字段。删除static
修饰符:
class Places {
static int id;
}
E.g:
class Places {
int id;
}
答案 1 :(得分:0)
可能您也希望保留在您添加到列表的循环中注销的循环。
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());
}