在课堂和活动之间传递arraylists

时间:2013-07-23 07:40:26

标签: android class arraylist share

我有一个让我发疯的问题。 我有一个类(不是活动),它扩展了Java类" Thread"。在这个cass中,我将一些PostJson变量下载到Run()方法中。

在这个Run()方法中,我调用了Object的构造函数,然后检查刚刚下载的Json变量,以便创建一个新的Object。 所有这一切都在于为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&#34; chargePointList我创建的所有新对象。

问题是,当进入另一个活动时,我调用此类来获取ArrayList,这将返回null。  如何在Class和其他活动之间共享填充的arraylist?

谢谢:)

2 个答案:

答案 0 :(得分:0)

从Activity类事件中调用GetChargePoint类run方法。所以你可以将列表返回到调用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{


}

答案 1 :(得分:0)

你可以让你的调用Activity实现某种类型的监听器接口,如下所示:

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传递给它。