0

我有一个小问题让我发疯。我有一个扩展 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。如何在班级和其他活动之间共享填充的数组列表?

谢谢 :)

4

2 回答 2

0

GetChargePoint 类的运行方法是从您的 Activity 类事件中调用的。因此您可以将列表返回到调用 Activity 类。然后您可以将列表从您的活动传递到另一个活动;

if(list!= null && list.size() > 0)
{
    Intent intent = new Intent(MainActivity.this,NextActList.class);
    intent .putExtra("list", list);                 
    intent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(drugIntent);
}else{


}
于 2013-07-23T07:47:10.293 回答
0

您可以使您的调用活动实现某种侦听器接口,如下所示:

interface ChargePointsDownloadListener {
    public void updateChartPoints(ArrayList newChargePoints);
}

和活动:

public class ChargePointActivity extends Activity implements ChargePointsDownloadListener
{   
    @Override
    public void updateChartPoints(ArrayList<ChargePoint_db> newChargePoints)
    {
    this.chargePointsList = newChargePoints;   
    }
}

从那时起,只需将 ChargePointsDownloadListener 实例传递给您的 GetChargePoint 构造函数。当您的线程执行时,请在侦听器上调用 updateChartPoints,然后将 GetChargePoint 类中的侦听器引用设置为 null - 否则 Activity 将泄漏,这不是一件好事。

另一种解决方案是使用 AsyncTask 而不是 Thread,但您仍然需要对 Activity 的引用才能将新的 ArrayList 传递给它。

于 2013-07-23T08:04:29.647 回答