0

I am sending JSONString from MainActivity to the GetLLRD class which is being sent to the server from MyAsyntask inner class in the GetLLRD class then I am getting ArrayList<ItemDTO> data object from the server which I want to pass to the map Activity.

How can I start the map Activity from the onPostExecute() method and pass the ArrayList<ItemDTO> data to it?

I appreciate any help.

GetRRLD class

public class GetLLRD {


    public void post_selected(String json) {
        new MyAsyncTask().execute(json);
    }

    class MyAsyncTask extends AsyncTask<String, Integer, List<ItemDTO>> {


        @Override
        protected List<ItemDTO> doInBackground(String... params) {

          .
          .
          .
          .

                Gson gson = new Gson();
                Type listType = new TypeToken<List<ItemDTO>>() {
                }.getType();
                ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
          .
          .
          .
          .     


            return null;

        }

        protected void onPostExecute(ArrayList<ItemDTO> result) {

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    new MyAsyncTask().execute();
                    System.out.println("The method onPostExcute() in GETLLRD class was invoked  again");
                }
            }, 1*30 * 1000);

            if (result != null) {
                Intent intent = new Intent(GetLLRD.this, Map.class);
                intent.putStringArrayListExtra("selected_route", result);
                startActivity(intent);              
                startActivity(new Intent(getApplicationContext(), Map.class));

            }

        }

    }
}

MapDataJSON class: import java.util.ArrayList;

public class MapDataJSON { ArrayList items;

public MapDataJSON(ArrayList<ItemDTO> items) {
    super();
    this.items = items;
}

public ArrayList<ItemDTO> getItems() {
    return items;
}

public void setItems(ArrayList<ItemDTO> items) {
    this.items = items;
}

public static class ItemDTO {
    double latitude;
    double longitude;
    int route;
    String direction;

    public ItemDTO(double latitude, double longitude, int route,
            String direction) {
        super();
        this.latitude = latitude;
        this.longitude = longitude;
        this.route = route;
        this.direction = direction;
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public int getRoute() {
        return route;
    }

    public String getDirection() {
        return direction;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public void setRoute(int route) {
        this.route = route;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }
}

}

Mr Asker
  • 2,300
  • 11
  • 31
  • 56

3 Answers3

1
Try this code : <br/> 

    public class GetLLRD {


        public void post_selected(Context context,String json) {
            new MyAsyncTask().execute(json);
        }

        class MyAsyncTask extends AsyncTask<String, Integer, List<ItemDTO>> {
    Context context;
    MyAsyncTask(Context context)
    {
    this.context=context;
    }
            @Override
            protected List<ItemDTO> doInBackground(String... params) {

              .
              .
              .
              .

                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<ItemDTO>>() {
                    }.getType();
                    ArrayList<ItemDTO> data = gson.fromJson(sb.toString(), listType);
              .
              .
              .
              .     


                return null;

            }

            protected void onPostExecute(ArrayList<ItemDTO> result) {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        new MyAsyncTask().execute();
                        System.out.println("The method onPostExcute() in GETLLRD class was invoked  again");
                    }
                }, 1*30 * 1000);

                if (result != null) {
                    Intent intent = new Intent(context, Map.class);
                    intent.putStringArrayListExtra("selected_route", result);
                    context.startActivity(intent);              


                }

            }

        }
    }
Rohit Heera
  • 2,709
  • 2
  • 21
  • 31
  • I am getting these error `The constructor Intent(GetLLRD, Class) is undefined` and `The method putStringArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, ArrayList)`? – Mr Asker Jul 06 '15 at 11:30
  • Thanks but what about the second error ? `The method putStringArrayListExtra(String, ArrayList) in the type Intent is not applicable for the arguments (String, ArrayList)` – Mr Asker Jul 06 '15 at 11:33
  • implement this to your model first: public class ItemDTO implements Serializable { private static final long serialVersionUID = 1L; – Rohit Heera Jul 06 '15 at 11:36
  • Then set list as : intent.putExtra("list",dataList); and get like : getIntent().getSerializableExtra("list"); – Rohit Heera Jul 06 '15 at 11:39
  • sorry just last question the context which I need it should be from GETRRLD or MainActivity or Map activity? – Mr Asker Jul 06 '15 at 11:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82487/discussion-between-rohit-heera-and-light). – Rohit Heera Jul 06 '15 at 11:45
  • It is the context of that class from where you call the method of post_selected(Context context). Are you getting me from where you pass the context i think its from MainActivity. – Rohit Heera Jul 06 '15 at 11:49
0

Have you maybe considered using a Getter / Setter methods for your ArrayList ?

public static List<String> getList() {
    return list;
}

public static setList(List<String> list) {
    this.list = list;
}

So what I mean is, once you have recieved your ArrayList from the server, you store it to a static Setter method.

setList(yourArrayList);

Then you start a new activity, and use a Getter method, pointing to your ArrayList in your previous activity.

PrevActivity.getList();

Not for copy / paste, but just a thought.

AndroidDev101
  • 122
  • 1
  • 13
0

You can implement Serializable interface in your modelClass ItemDTO and then pass serializableArraylist using intent

Rohit Heera
  • 2,709
  • 2
  • 21
  • 31