什么是将一个片段传递给另一个片段的JSON数据的最佳方法

时间:2014-12-09 04:49:14

标签: android json android-fragments android-asynctask sugarcrm

我在站点中有两个片段,我的第一个片段调用AsyncTask并获得一些json值。然后我想将我的JSON值传递给我的第二个Fragment的TextView。在片段之间传递json值的最佳方法是什么?

这是AsyncTask

中的Fragment 1
class LoadingAccountEntry extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected String doInBackground(String... args) {
            String response;

            Map<String, Object> data = new LinkedHashMap<String, Object>();
            data.put(SESSION, sessionId);
            data.put(MODULE_NAME, module_name);
            data.put(ID, entry_id != null ? entry_id : "");
            data.put(
                    SELECT_FIELDS,
                    (selectFields != null && selectFields.length != 0) ? new JSONArray(
                            Arrays.asList(selectFields)) : "");

            try {
                JSONArray nameValueArray = new JSONArray();
                if (linkNameToFieldsArray != null
                        && linkNameToFieldsArray.size() != 0) {
                    for (Entry<String, List<String>> entry : linkNameToFieldsArray
                            .entrySet()) {
                        JSONObject nameValue = new JSONObject();
                        nameValue.put("name", entry.getKey());
                        nameValue.put("value", new JSONArray(entry.getValue()));
                        nameValueArray.put(nameValue);
                    }
                }
                data.put(LINK_NAME_TO_FIELDS_ARRAY, nameValueArray);
                String restData = org.json.simple.JSONValue.toJSONString(data);

                HttpClient httpClient = new DefaultHttpClient();
                HttpPost req = new HttpPost(restURL);
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair(METHOD, GET_ENTRY));
                nameValuePairs.add(new BasicNameValuePair(INPUT_TYPE, JSON));
                nameValuePairs.add(new BasicNameValuePair(RESPONSE_TYPE, JSON));
                nameValuePairs.add(new BasicNameValuePair(REST_DATA, restData));
                req.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Send POST request
                httpClient.getParams().setBooleanParameter(
                        CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
                HttpResponse res = httpClient.execute(req);
                response = EntityUtils.toString(res.getEntity());
                // Log.d("Responce", response.toString());

                if (response == null) {
                    Log.e("Error !", "faild to connect !");
                }

                JSONObject responseObj = new JSONObject(response);
                JSONArray jArray = responseObj.getJSONArray(ENTRY_LIST);

                for (int i = 0; i < jArray.length(); i++) {

                    JSONObject obj = jArray.getJSONObject(i);
                    // Log.d("obj", obj.toString());
                    // JSONObject objName = new JSONObject("name");
                    id = obj.getString("id");
                    Log.d("id", id);

                    JSONObject name_value_list = obj
                            .getJSONObject("name_value_list");

                    JSONObject assignedUserName = name_value_list
                            .getJSONObject("assigned_user_name");
                    assigned_user_name = assignedUserName.getString("value");
                    Log.d("assigned_user_name", assigned_user_name);

                    JSONObject modifiedByName = name_value_list
                            .getJSONObject("modified_by_name");
                    modified_by_name = modifiedByName.getString("value");
                    Log.d("modified_by_name", modified_by_name);


                }

            } catch (JSONException e) {

            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

        }

Fragment 2

public class Fragment_account_details extends Fragment {

    TextView name, officePhone,fax;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_account_details, container,false);

        name = (TextView) rootView.findViewById(R.id.text_name);
        fax = (TextView) rootView.findViewById(R.id.text_fax);


        return rootView;


    }

1 个答案:

答案 0 :(得分:4)

您可以使用bundle将数据传递给Fragment_account_details,如下所示:

            Fragment fragment = new Fragment_account_details();
            Bundle bundle = new Bundle();
            bundle.putString("name", name);
            fragment.setArguments(bundle);

现在你的Fragment_account_details onCreateView就像:

  String name= getArguments().getString("name");

像这样你可以传递值。这会有所帮助。