如何在 Android 中加载页面时设置值?

How to set a value when the page loads in Android?

我正在创建一个 Android 应用程序,但我卡在了某个点上,不知道该怎么做。我正在调用网络服务并获得 JSON 值。然后我将该值设置为 TextView 字段。但我面临的问题是当页面加载时,将值设置为 TextView 已经晚了。有时该值未设置为 TextView。所以我必须在继续另一个函数之前检查它,如果该值未设置为然后再次调用以下方法。

调用网络服务并获取值并将文本设置为 TextValue

private void getJsonRequest() {
    String tag_string_req = "req_data";

    request = new StringRequest(Request.Method.POST, "URL", new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                if (jsonObject.names().get(0).equals("res")) {

                    JSONArray array = jsonObject.getJSONArray("res");
                    JSONObject obj = array.getJSONObject(0);
                    String value = obj.getString("value");

                    spin.setText(value);
                } else {
                    Toast.makeText(getApplicationContext(), "No Value Available", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            hashMap = new HashMap<String, String>();
            hashMap.put("uid", userID);
            return hashMap;
        }
    };
    request.setRetryPolicy(new DefaultRetryPolicy(15 * 1000, 1,
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
    AppController.getInstance().addToRequestQueue(request, tag_string_req);
}

然后我在onCreate()里面调用了那个方法。

protected void onCreate(Bundle savedInstanceState) {
   //There are more codes,
   getJsonRequest();
}

所以我需要的是在页面加载时设置该值。在我看到必须设置该值的页面之前。我怎样才能做到这一点?

您可以创建一个 progress dialog 来向用户显示您正在获取数据

  //initialize the progress dialog and show it
  progressDialog = new ProgressDialog(getActivity());
  progressDialog.setMessage("Getting data..");
  progressDialog.show();

这会在发送之前 request

然后在 onResponse 中设置 textView 中的文本后,你称之为

  progressDialog.dismiss();

您还应该关闭 onErrorResponse

中的进度对话框

编辑:

如果你应该等待返回的值 JSON 因为你将在进一步的实现中得到这个值你可以设置一个 boolean 标志这个 this

  private boolean mHasReceivedData = false;

然后在onCreate方法中,你这样改

  protected void onCreate(Bundle savedInstanceState) {
   //There are more codes,
   getJsonRequest();

   if(mHasReceivedData){
    //here you use the returned data.

} }

然后在请求成功时添加这一行

  mHasReceivedData = true;

这样您可以确保返回的值 JSON 在使用它之前设置好,因此不会有空的 textView。

private void getJsonRequest() {
String tag_string_req = "req_data";

request = new StringRequest(Request.Method.POST, "URL", new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
      progressDialog.dismiss();
        try {
            JSONObject jsonObject = new JSONObject(response);
            if (jsonObject.names().get(0).equals("res")) {

                JSONArray array = jsonObject.getJSONArray("res");
                JSONObject obj = array.getJSONObject(0);
                String value = obj.getString("value");

                spin.setText(value);
            } else {
                Toast.makeText(getApplicationContext(), "No Value Available", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        progressDialog.dismiss();
        error.printStackTrace();
    }
}) {
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        hashMap = new HashMap<String, String>();
        hashMap.put("uid", userID);
        return hashMap;
    }
};
request.setRetryPolicy(new DefaultRetryPolicy(15 * 1000, 1,
    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Fetching The File....");
progressDialog.show();
AppController.getInstance().addToRequestQueue(request, tag_string_req);
}

我建议的最简单的解决方案是创建一个对象并在其上同步,这将保证在您的用户看到页面之前设置 textview,但这样做是一种不好的做法。