【问题标题】:Start activity from onPostExecute() method and pass the arrayList to it从 onPostExecute() 方法启动活动并将 arrayList 传递给它
【发布时间】:2015-07-06 11:09:29
【问题描述】:

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

如何从onPostExecute() 方法启动map Activity 并将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 类: 导入 java.util.ArrayList;

公共类 MapDataJSON { 数组列表项;

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

}

【问题讨论】:

标签: android


【解决方案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);              


                }

            }

        }
    }

【讨论】:

  • 我收到这些错误 The constructor Intent(GetLLRD, Class&lt;Map&gt;) is undefinedThe method putStringArrayListExtra(String, ArrayList&lt;String&gt;) in the type Intent is not applicable for the arguments (String, ArrayList&lt;MapDataJSON.ItemDTO&gt;)?
  • 谢谢,但是第二个错误呢? The method putStringArrayListExtra(String, ArrayList&lt;String&gt;) in the type Intent is not applicable for the arguments (String, ArrayList&lt;MapDataJSON.ItemDTO&gt;)
  • 首先在你的模型中实现这个:public class ItemDTO implements Serializable { private static final long serialVersionUID = 1L;
  • 然后将列表设置为:intent.putExtra("list",dataList);并得到:getIntent().getSerializableExtra("list");
  • 对不起,我需要的上下文应该来自 GETRRLD 或 MainActivity 或 Map 活动?
【解决方案2】:

您是否考虑过为您的 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();

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

    • 如何从非活动类开始活动?
    • 我会为您提供解决方案。
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2014-03-07
    相关资源
    最近更新 更多