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

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

本文介绍了将JSON数据从一个片段传递到另一个片段的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我在Fragment 1

中的AsyncTask

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;


    }
解决方案

您可以使用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");

您可以像这样传递值.希望有帮助.

I have two fragments in site my first fragment call AsyncTask and get some json values.and then i want to pass my JSON values to my second Fragment's TextViews.What is the best way pass json value between fragments?

this is my AsyncTask in 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) {

        }

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


    }
解决方案

You can use bundle to pass data to Fragment_account_details like this :

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

now in your Fragment_account_details onCreateView do like :

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

like this you can pass values.hope this helps.

这篇关于将JSON数据从一个片段传递到另一个片段的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:29