将数据从片段发送到活动并解析json

时间:2013-09-06 14:12:32

标签: java android json android-fragments

我正在尝试从我的片段活动向活动发送'id'并根据此'id'字段解析json数据。

我成功地将数据从fragment发送到TourDetail活动,但它没有基于相同的'id'解析json。当我使用toasts检查它时,代码没有到达第二个try块。

虽然当我从其他一些活动传递'id'时,同样的活动(TourDetailActivity.java)工作正常。比方说,如果我从jsonuseactivity.java传递'id',它将被TourDetailActivity.java成功接收,并生成相应的json结果并显示相应的结果。

但是如果从片段活动传递相同的'id',它虽然接收'id'但没有生成json,并且没有生成基于json的必需结果。

TourDetailActivity.java

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tour_detail);

    Intent intent = getIntent();
    String rcvid = intent.getStringExtra("id");
    Toast.makeText(getApplicationContext(), rcvid, Toast.LENGTH_LONG).show();

    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("tourid", rcvid));

    String response = null;

    // call executeHttpPost method passing necessary parameters 
    try {
        Toast.makeText(getApplicationContext(), "in try block", Toast.LENGTH_LONG).show();
        response = CustomHttpClient.executeHttpPost(
                //"http://129.107.187.135/CSE5324/jsonscript.php", // your ip address if using localhost server
                "http://www.futurolicht.com/jsontour.php",  // in case of a remote server
                postParameters);
        String result = response.toString();
        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
        //parse json data
        try {
            Toast.makeText(getApplicationContext(), "in 2nd try block", Toast.LENGTH_LONG).show();
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {

                JSONObject json_data = jArray.getJSONObject(i);
                tname = (TextView) findViewById(R.id.txt_tourtitle);
                td = (TextView) findViewById(R.id.txt_day);
                tn = (TextView) findViewById(R.id.txt_night);
                tdest = (TextView) findViewById(R.id.txt_tourdetail_dest);
                btn = (Button) findViewById(R.id.btn_tourdetail);
                btn.setOnClickListener(this);

                tname.setText(json_data.getString("tour_name"));
                tn.setText(json_data.getString("nights"));
                td.setText(json_data.getString("days"));
                name = json_data.getString("tour_name");
                n = json_data.getString("nights");
                d = json_data.getString("days");
            }
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }
    }

这是我的Fragmentone.java的onCreate()方法代码:

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
        String fontPath = "fonts/ox.ttf";
        b1 = (Button)root.findViewById(R.id.featured_btn1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String id = "34";
                Intent intent = new Intent(getActivity(), TourDetailActivity.class);
                intent.putExtra("id", id);
                getActivity().startActivity(intent);
            }
        });

1 个答案:

答案 0 :(得分:0)

我不知道实际发生了什么,因为上面的代码对我来说似乎没问题。您可以将变量设置为Static,而不是发送数据,并在Fragmentone.id等其他类中访问该变量。这肯定会奏效。

您是否也尝试在id而不是onStart()中抓住onCreate()?检查在启动时是否有传递给片段的参数很方便

相关问题