簡體   English   中英

Volley請求隊列在Android片段中,未在文本視圖中設置json數組數據

[英]Volley request Queue in android fragment, not setting json array data in text view

我正在嘗試將從json數組解析的值插入文本視圖。 當我在調試器上運行該應用程序時,似乎我想要的所有信息均已存在,但是未在文本視圖中設置(文本視圖返回空白)。

我認為可能是因為我試圖在一個片段中運行Volley請求隊列,同時錯誤地調用了上下文。

mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

但是,我仍在學習,因此我不確定如何進行修改。 完整的類代碼如下。

public class BookingHistoryFragment extends Fragment {


private TextView bookingHistoryTV;
private RequestQueue mQueue;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View view = inflater.inflate(R.layout.fragment_booking_history, container, false);
    bookingHistoryTV = view.findViewById(R.id.bookingHistoryTV);

    mQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
    jsonParse();

    return inflater.inflate(R.layout.fragment_booking_history, container, false);
}

private void jsonParse() {
    String url = "http://178.128.166.68/getBookingHistory.php";

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("bookingHistory");

                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject bookingHistory = jsonArray.getJSONObject(i);

                            String fromLocality = bookingHistory.getString("fromLocality");
                            String toLocality = bookingHistory.getString("toLocality");
                            double cost = bookingHistory.getDouble("cost");
                            String date = bookingHistory.getString("date");

                            String parsed = fromLocality + " | " + toLocality + " | " + String.valueOf(cost) + " | " + date + "\n\n";
                            bookingHistoryTV.append(parsed);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    mQueue.add(request);
}
}

對於我要去哪里的任何反饋,我們將不勝感激,不勝感激。

聲明String parsed; 全局並在JsonObjectRequest for循環內分配值,並在循環外分配setText值,如下所示。

public class BookingHistoryFragment extends Fragment {

  String parsed; //Defined Globally
  private TextView bookingHistoryTV;
  private RequestQueue mQueue;

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
  @Nullable Bundle savedInstanceState) {


  View view = inflater.inflate(R.layout.fragment_booking_history, container, false);
  bookingHistoryTV = view.findViewById(R.id.bookingHistoryTV);
  jsonParse();

  return inflater.inflate(R.layout.fragment_booking_history, container, false);
  }

  private void jsonParse() {
  String url = "http://178.128.166.68/getBookingHistory.php";

  JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray jsonArray = response.getJSONArray("bookingHistory");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject bookingHistory = jsonArray.getJSONObject(i);

                        String fromLocality = 
                         bookingHistory.getString("fromLocality");
                        String toLocality = bookingHistory.getString("toLocality");
                        double cost = bookingHistory.getDouble("cost");
                        String date = bookingHistory.getString("date");

                        parsed = fromLocality + " | " + toLocality + " | " + String.valueOf(cost) + " | " + date + "\n\n";
                    }

                  bookingHistoryTV.setText(parsed); //setText outside of For Loop

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
    }
});

  //creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

 //adding the string request to request queue
    mQueue.add(request);
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM