在OnCreateView上的片段中访问局部变量

时间:2018-07-21 23:38:14

标签: android-studio android-fragments

我正在尝试访问“ existingTransactionID”,该值由方法“ existingTransaction(userID);”分配。为确保已分配,我尝试在AlertDialogBox上显示它,但它显示为空白。但是,如果我尝试付款,它似乎已分配给方法“ saveTransaction();”。当我放置AlertDialogBox检查是否已分配时,在“ onActivityResult”上调用该函数。

private String existingTransactionID = "";

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
{
    LayoutInflater lf = getActivity().getLayoutInflater();
    rootView = lf.inflate(R.layout.consultation_fragment, container, false);

    existingTransaction(userID);

    //The assigned value is not displayed here
    AlertDialogBox("TEST", existingTransactionID);

    return rootView;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    mPayMayaCheckout.onActivityResult(requestCode, resultCode, data);

    //The assigned value is displayed here
    AlertDialogBox("TEST", existingTransactionID);

    saveTransaction(existingTransactionID, userID, therapistID, payment, transactionStatus);
}

public void existingTransaction(final String UID)
{
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL2,
            new Response.Listener<String>()
            {
                @Override
                public void onResponse(String response)
                {
                    try
                    {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString("success");
                        String message = jsonObject.getString("message");
                        JSONArray jsonArray = jsonObject.getJSONArray("transaction");
                        if(success.equals("1"))
                        {
                            AlertDialogBox("Success", message);
                            for(int i = 0; i < jsonArray.length(); i++)
                            {
                                JSONObject object = jsonArray.getJSONObject(i);
                                String transactionID = object.getString("TransactionID");
                                String transactionStatus2 = object.getString("TransactionStatus");

                                existingTransactionID = transactionID;
                                transactionStatus = transactionStatus2;
                            }
                        }
                        else
                        {
                            AlertDialogBox("ERROR", message);
                        }
                    }
                    catch (JSONException e)
                    {
                        e.printStackTrace();
                        AlertDialogBox("ERROR", e.toString());
                    }

                    final String result=response;
                    Log.d("response", "result : "+result);
                }
            },
            new Response.ErrorListener()
            {
                @Override
                public void onErrorResponse(VolleyError error)
                {
                    AlertDialogBox("ERROR", error.toString());
                }
            })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError
        {
            Map<String, String> params = new HashMap<>();
            params.put("UserID", UID);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
    requestQueue.add(stringRequest);
}

0 个答案:

没有答案