从onPostExecute()方法启动活动并将arrayList传递给它

时间:2015-07-06 11:09:29

标签: android

我将JSONStringMainActivity发送到GetLLRD类,该类从MyAsyntask类中的GetLLRD内部类发送到服务器然后我我从服务器获取ArrayList<ItemDTO> data对象,我想传递给map Activity

如何从map Activity方法启动onPostExecute()并将ArrayList<ItemDTO> data传递给它?

我感谢任何帮助。

GetRRLD类

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类: import java.util.ArrayList;

public class MapDataJSON {     ArrayList项目;

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

}

3 个答案:

答案 0 :(得分: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);              


                }

            }

        }
    }

答案 1 :(得分:0)

您是否考虑过为ArrayList使用Getter / Setter方法?

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

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

所以我的意思是,一旦你从服务器收到了ArrayList,你就将它存储到一个静态的Setter方法。

setList(yourArrayList);

然后您开始一项新活动,并使用Getter方法,指向您之前活动中的ArrayList

PrevActivity.getList();

不是复制/粘贴,而只是一个想法。

答案 2 :(得分:0)

您可以在modelClass ItemDTO中实现Serializable接口,然后使用intent传递serializableArraylist

相关问题