如何将当前页面json对象值解析为下一页文本字段?以使用以前的页面键值设置textview

时间:2016-05-31 11:13:47

标签: android json

      private void checkOut() {
        ArrayList<FishInfoFromJSON> fishInfoFromJSONs = new ArrayList<>();
        fishInfoFromJSONs = ((MainActivity) mContext).getAddedItems();

        final ArrayList<CartInfo> cartInfos = new ArrayList<>();

        for (FishInfoFromJSON fishInfoFromJSON : fishInfoFromJSONs) {
            CartInfo cartInfo = new CartInfo();
            cartInfo.setProductId(fishInfoFromJSON.getProductId());
            cartInfo.setQuantity(fishInfoFromJSON.getQuantitySelected());
            cartInfos.add(cartInfo);        }

        Customer customer = new Customer();
        customer.setCustomer_id(12);
        customer.setFirstname(firstname.getText().toString());
        customer.setLastname(lastname.getText().toString());
       // customer.setEmail("rajagopal.viswanathan@gmail.com");
        SharedPreferences pref =this.getActivity() .getSharedPreferences("MyPref", Context.MODE_PRIVATE);
        String emailst = pref.getString("userEmail", " ");
       // String emailst = pref.getString("userEmail", FragmentLogin.emailedt.edtstr);
        customer.setEmail(emailst.toString());
        customer.setStreet1(addr1.getText().toString());
        customer.setStreet2(addr2.getText().toString());
        customer.setCity("Coimbatore");
        customer.setRegion("TamilNadu");
        customer.setCountry("India");
        customer.setPostcode(post_code.getText().toString());
        customer.setPhone(phonenum.getText().toString());
        customer.setFax("7298292922");

        String text = addtext.getText().toString();
        String spinn = preferredTimeSpinner.getSelectedItem().toString();

        final ShippingInfo shippingInfo = new ShippingInfo();
        shippingInfo.setCart(cartInfos);
        shippingInfo.setCustomer(customer);
        shippingInfo.setNotes(text);
        shippingInfo.setPreferredDelTime(spinn);
        Log.i("shipping information" , String.valueOf(shippingInfo));

        final   JSONObject jsonObj = new JSONObject();
        try {
            jsonObj.put("cart", cartInfos);
            jsonObj.put("customer", customer);
            jsonObj.put(" ", text);
            jsonObj.put(" ", spinn);

         /*   JSONObject jObj = new JSONObject(String.valueOf(jsonObj) );
            Toast.makeText(getActivity(),jObj.toString(),Toast.LENGTH_SHORT).show();
*/
                    } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



        Gson gson = new Gson();
        String str_json = gson.toJson(shippingInfo);
        Log.i("gopi", str_json);

        HttpEntity entity = new StringEntity(str_json, "UTF-8");
        String contentType = "application/json";
        String url = EndPoints.checkOutUrl;
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(mContext, url, entity, contentType, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

                String responseStr = "nothing gopi";
                try {
                    responseStr = new String(responseBody, "UTF-8");

                } catch (UnsupportedEncodingException e) {

                }

                Log.i("HTTP", "onSuccess: " + statusCode + "   responseBody:" + responseStr);

                Toast.makeText(mContext, "Order placed successfully!", Toast.LENGTH_SHORT).show();
                Toast.makeText(getActivity(), jsonObj.toString(), Toast.LENGTH_LONG).show();
                String result = firstname.getText().toString()
                        + lastname.getText().toString()
                        + addr1.getText().toString()
                        + addr2.getText().toString()
                        + addtext.getText().toString()
                        + phonenum.getText().toString()
                        + preferredTimeSpinner.getSelectedItem().toString();

                Toast.makeText(getActivity(),result.toString(),Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                String responseStr = "nothing gopi";

                if (responseBody != null) {
                    try {
                        responseStr = new String(responseBody, "UTF-8");
                    } catch (UnsupportedEncodingException e) {

                    }

                    Log.i("HTTP", "onFailure: " + statusCode + "   responseBody:" + responseStr);
                    Toast.makeText(mContext, "Failed! Please contact Customer Support!" + statusCode, Toast.LENGTH_SHORT).show();
                }

            }
        });

我必须使用像name, order id, date, price etc...这样的json对象值设置下一页文本字段。我不知道该怎么做,你能给一些想法来为textview分配json值吗?

下一页代码:

public class Order_conformation extends AppCompatActivity implements View.OnClickListener {
    private Button placeorder;
    private TextView cus_id,order_date,cus_name,cus_email,cust_name,cus_add,cus_mobile,fixedrate,quantity,price,subtotal,tax,discount,grandtotal,subtotallast,
shippingcharge,total;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.order_conformation_page);
    Button placeorder= (Button)findViewById(R.id.btn_placeorder);

    /* SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
     String cusname = pref.getString("firstname", " ");
     String phnenumber = pref.getString("phone", " ");
     String addr = pref.getString("street1", " ");
     String cusemail = pref.getString("email", " ");*/

     cus_id=(TextView)findViewById(R.id.customer_id);
     order_date=(TextView)findViewById(R.id.order_date);
     cus_name=(TextView)findViewById(R.id.customer_name);
     cust_name = (TextView)findViewById(R.id.customer_name1);
     cus_email=(TextView)findViewById(R.id.customer_email);
     cus_add=(TextView)findViewById(R.id.customer_address);
     cus_mobile=(TextView)findViewById(R.id.customer_number);
     fixedrate = (TextView)findViewById(R.id.first_name);
     quantity = (TextView)findViewById(R.id.qty);
     price = (TextView)findViewById(R.id.price);
     subtotal = (TextView)findViewById(R.id.subtotal);
     tax = (TextView)findViewById(R.id.tax);
     discount = (TextView)findViewById(R.id.discount);
     grandtotal = (TextView)findViewById(R.id.rowtotal);
     subtotallast = (TextView)findViewById(R.id.subtotal_last);
     shippingcharge = (TextView)findViewById(R.id.shipping_charge);
     total = (TextView)findViewById(R.id.grand_total);
     placeorder.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(Order_conformation.this, Order_conformation_final.class);
            final OrderInfo orderInfo = new OrderInfo();
            startActivity(intent);

        }

    });

}

@Override
public void onClick(View view) {

}

}

1 个答案:

答案 0 :(得分:0)

您可以将Json作为字符串传递或将其解析为POJO并使用此传递给下一个Activity,

intent.putExtra("key","value");

然后您可以在下一个活动中检索它,如下所示

getIntent().getStringExtra("key");