我有一个小问题让我发疯。我有一个扩展 Java 类“线程”的类(不是活动)。在这个案例中,我将一些 PostJson 变量下载到 Run() 方法中。
在这个 Run() 方法中,我调用一个 Object 的构造函数,并检查他刚刚下载的 Json 变量,以便创建一个新的 Object。所有这些都在一个 for cicle 中,为 JsonPost 中的每个集合创建一个新对象。
public class GetChargePoint extends Thread{
JSONObject obj;
JSONArray jArray;
InputStream inputStream = null;
Handler handler;
private ConnectionSource connectionSource;
public ArrayList<ChargePoint_db> chargePointList = null;
public GetChargePoint(Handler handler) {
super();
this.handler = handler;
try {
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
@Override
public void run() {
...
for (int i = 0; i < jArray.length(); i++) {
try {
JSONObject oneObject = jArray.getJSONObject(i);
String name = oneObject.getString("Name");
String address = oneObject.getString("Address");
String description = oneObject.getString("Description");
String ownerTelNumber = oneObject
.getString("OwnerTelNumber");
String serialNumber = oneObject.getString("SerialNumber");
String dateModified = oneObject.getString("DateModified");
String id = oneObject.getString("Id");
int pointModelId = oneObject.getInt("PointModelId");
int maxNominalPowerKw = oneObject
.getInt("MaxNominalPowerkW");
boolean hasAcceleratedCharge = oneObject
.getBoolean("HasAcceleratedCharge");
boolean isInMaintenance = oneObject
.getBoolean("IsInMaintenance");
boolean active = oneObject.getBoolean("Active");
boolean isDeleted = oneObject.getBoolean("IsDeleted");
boolean hasFastCharge = oneObject
.getBoolean("HasFastCharge");
double latitude = oneObject.getDouble("Latitude");
double longitude = oneObject.getDouble("Longitude");
//calling the constructor.
Object object= new Object(id,
serialNumber, name, description, address,
maxNominalPowerKw, hasAcceleratedCharge,
hasFastCharge, ownerTelNumber, false, pointModelId,
active, isInMaintenance, 0, dateModified, latitude,
longitude);
chargePoint.save(repo);
chargePointList.add(object);
...
到目前为止,一切都很好。但正如你所看到的,我想将我创建的所有新对象都填入一个 ArrayList "chargePointList。
问题是当进入另一个活动时,我调用这个类来获取 ArrayList,这个返回 null。如何在班级和其他活动之间共享填充的数组列表?
谢谢 :)