我有以下代码和下面描述的问题,带有一个arraylist:
class WP{
double id, type;
String shortname, name;
}
public class GPSAct extends Activity {
...
public ArrayList<WP> waypoints = new ArrayList<WP>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
getData(where);
for(int i=0;i<waypoints.size();i++){
Toast.makeText(getApplicationContext(), waypoints.get(i).shortname, Toast.LENGTH_LONG).show();
}
}
public void getData(String where) {
WP wps = new WP();
try {
openDataBase();
Cursor cur = myDataBase.query(TABLE_WP, null, where, null, null, null, null);
cur.moveToFirst();
waypoints.clear();
for (int i = 0; i < cur.getCount(); i++) {
wps.id = cur.getDouble(0);
wps.type = cur.getDouble(0);
wps.shortname = cur.getString(1);
wps.name = cur.getString(2);
waypoints.add(i, wps);
}
cur.close();
...
问题是,当我尝试访问航点数组列表(使用 waypoints.get(i),我从 0 开始一直到 waypoints.size())时,数组的所有元素都具有相同的值(最后的值在 getData()) 中添加的 wps 元素。