如何在android中将arraylist变量从一个类调用到另一个类

时间:2013-11-18 09:19:08

标签: java android json arraylist hashmap

我有疑问,如何在Android中将arraylist变量从一个类调用到另一个类,我是json服务的arryalist形式的框架,我在很多方面尝试过,但我不知道如何调用数组列表从一个班级到另一个班级。我使用以下代码。使用HashMap构建数组列表,请提前帮助我,Thnaks。

下面显示的是FirstActivityCLass

protected Integer doInBackground(Void... arg0) {

            try {

                JSONObject e = null;
                System.out.println("In Background...");
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                        .detectAll().penaltyLog().build();
                StrictMode.setThreadPolicy(policy);
                String API = "http://traininglabs.com/nic/a-remainders-list.php?email="+email;
                System.out.println(API);
                JSONObject loginjson = JSONfunctions1.getJSONfromURL(API);
                responseArray=loginjson.getJSONArray("user");

                mylist=new ArrayList<HashMap<String,String>>();
                for (int i = 0; i < responseArray.length(); i++)
                        {
                    map = new HashMap<String, String>();                     

                JSONObject obj= responseArray.getJSONObject(i);

                    **String ss = obj.getString("id");
                    map.put("id", obj.getString("id"));**
                    map.put("state", obj.getString("status"));
                    map.put("entity", obj.getString("company"));
                    map.put("date", obj.getString("adate"));
                     s1 = e.getString("state");
                     s2 = e.getString("entity");
                     s3 = e.getString("date");
                    System.out.println("this is string--->" + ss);
                    mylist.add(map);
                }

            } catch (Exception e) {
                Log.d("Error", "Error in API" + e.getStackTrace().toString());
                return 1;
            }
            return 0;
        }

ArryaList的第二个Activity参数是id

protected Integer doInBackground(Void... arg0) {

            System.out.println("In Background...");
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .detectAll().penaltyLog().build();
    StrictMode.setThreadPolicy(policy);

            try {
                String ssp_state = sp_state.getSelectedItem().toString();
                String ssp_entity = sp_entity.getSelectedItem().toString();
                String ssp_date = sp_date.getText().toString();
                String ssp_time = sp_time.getText().toString();

                String API = "http://traininglabs.com/projects/nic/a-edit-remainder.php?id="+**id**+ "&email="+ mail_str+ "&sid="+ sp_id+ "entity="+ ssp_entity+datetime="+ ssp_date+ ssp_time;
                System.out.println(API);

                JSONArray loginjson = JSONfunctions.getJSONfromURL(API);

                Log.d(TAG, "JSON_RES" + loginjson);

                return 0;

            } catch (Exception e) {
                Log.d("Error", "Error in API" + e.getStackTrace().toString());
                return 1;

            } // return null;

        }

1 个答案:

答案 0 :(得分:3)

将其设为全局,或将其置于单一,更复杂但持久的方式,因为您正在通过共享首选项处理网络共享。