我正在尝试将从 json 数组解析的值插入到 TextView 中。当我在调试器上运行应用程序时,似乎我想要的所有信息都在那里,但它没有在 TextView 中设置( TextView 返回空白)。
我认为这可能是因为我试图在片段中运行 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);
}
}
关于java - android片段中的Volley请求队列,不在 TextView 中设置json数组数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51644082/