0

I have a little problem that is driving me crazy. I have a Class (NOT AN ACTIVITY) which extends the Java class "Thread". In this cass I download some PostJson variables into the Run() method.

in this Run() method I call the constructor of an Object and I check him the just downloaded Json variables so that I create a new Object. All of this in a for cicle that for each collection in JsonPost, create a new object.

  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);

...

So far so good. But as you can see, I want to fill into an ArrayList "chargePointList all new object I create.

The problem is that when into another activity I call this class to get the ArrayList, this return null. How can I share filled arraylist between a Class and other activities??

thanks :)

Pikkio
  • 19
  • 2
  • 7

2 Answers2

0

GetChargePoint class run method is called from your Activity class event . so you can return list into calling Activity class. Then you can pass list from your activity to another 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{


}
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37
0

You can make your calling Activity implement some sort of listener interface, like so:

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

And the Activity:

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

And from then on, just pass a ChargePointsDownloadListener instance to your GetChargePoint constructor. When your thread executes, call updateChartPoints on the listener and then set your listener reference in the GetChargePoint class to null - otherwise the Activity will leak and that's not a good thing.

Another solution would be to use AsyncTask instead of Thread, but you would still need a reference to your Activity in order to pass it the new ArrayList.

npace
  • 4,218
  • 1
  • 25
  • 35
  • Thank you :) I'm trying your solution! but if I need to get all the ArrayList, how can I do? @npace – Pikkio Jul 23 '13 at 09:10
  • What do you mean get all the ArrayList? After the for loop just call updateChartPoints. – npace Jul 23 '13 at 10:05
  • I did it but the error is null pointer exception... during the debug the arraylist return null :( – Pikkio Jul 23 '13 at 12:16