gpt4 book ai didi

java - RecyclerView 并不总是加载 JSON 数据

转载 作者:行者123 更新时间:2023-12-02 03:07:39 24 4
gpt4 key购买 nike

我正在学习编写 Android 应用程序。尝试解析 json 以将结果保存在 RecyclerView 中,在跨 Activity 或按主页按钮然后返回时,有时会加载这些结果,有时则不会加载。对于 http 调用,我使用 OkHttp。我尝试过其他解决方案,但没有一个解决方案解决了我的问题,即使由于我是初学者,我可能在操作逻辑上做错了什么。

public class VehiclesListFragment extends Fragment {  

...

private OkHttpClient httpClient = new OkHttpClient();
final List<Vehicle> vehicles = new ArrayList<>();
private String url = "myurl";
final Request request = new Request.Builder().url(url).build();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.vehicles_recycler_view, container, false);

VehicleRecyclerView = (RecyclerView)view.findViewById(R.id.vehicles_recycler_view);
VehicleRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

VehicleRecyclerView.addItemDecoration(new DividerItemDecoration(getContext(),
DividerItemDecoration.VERTICAL));

updateUI();

return view;

}


private void updateUI() {

httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("Error", "Error!");
}
@Override
public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {
if(response.isSuccessful()){
final String body = response.body().string();

try {
JSONObject Jobject = new JSONObject(body);
JSONArray Jarray = Jobject.getJSONArray("models");
for (int i = 0; i < Jarray.length(); i++) {
Vehicle vehicle = new Vehicle();
JSONObject Jobj = Jarray.getJSONObject(i);
vehicle.setName(Jobj.getString("Name"));
vehicle.setBrand(Jobj.getString("Brand"));
plane.setYear(Jobj.getString("Year"));
vehicles.add(vehicle);
}
} catch (JSONException e) {e.printStackTrace();}
}
}
});

Adapter = new PlaneAdapter(planes);
PlaneRecyclerView.setAdapter(Adapter);
Adapter.notifyDataSetChanged();
}

RecyclerView 有时加载,有时不加载,特别是在跨 Activity 或按主页并恢复时。

我尝试移动适配器构造函数的位置以及设置和错误更改,但我不明白我缺少什么。如果您需要更多代码,我会提供。

谢谢。

最佳答案

您应该在回调中更新回收器 View 。

private void updateUI() {

httpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d("Error", "Error!");
}

@Override
public void onResponse(@NotNull Call call, @NotNull final Response response) throws IOException {
if (response.isSuccessful()) {
final String body = response.body().string();
vehicles.clear();// Removes all of the elements
try {
JSONObject Jobject = new JSONObject(body);
JSONArray Jarray = Jobject.getJSONArray("models");
for (int i = 0; i < Jarray.length(); i++) {
Vehicle vehicle = new Vehicle();
JSONObject Jobj = Jarray.getJSONObject(i);
vehicle.setName(Jobj.getString("Name"));
vehicle.setBrand(Jobj.getString("Brand"));
vehicle.setYear(Jobj.getString("Year"));
vehicles.add(vehicle);
}
} catch (JSONException e) {
e.printStackTrace();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (null == Adapter) {
Adapter = new PlaneAdapter(vehicles);
PlaneRecyclerView.setAdapter(Adapter);
} else {
Adapter.notifyDataSetChanged();
}
}
});
}
}
});
}

关于java - RecyclerView 并不总是加载 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57014987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com