gpt4 book ai didi

java - 将事件添加到 "Material-Calendar-View"插件的日历时出现问题

转载 作者:行者123 更新时间:2023-12-02 04:44:47 25 4
gpt4 key购买 nike

我正在尝试从服务器获取 JSON 响应,然后解析它,并将一些日期事件添加到 Material-Calendar-View 插件的日历中。

它几乎可以工作,但是日历的日期装饰不是立即添加的,而是在滚动日历中的 View 页面之后添加的。

我有一个想法,我应该将所有代码放到另一个线程中,因为有一个异常

Skipped 31 frames! The application may be doing too much work on its main thread.

但我不确定是否应该这样做,如果应该的话,我也不知道如何做对。

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
events = new ArrayList<>();
calendarView = (CalendarView) getView().findViewById(R.id.calendarView);
mRequestQueue = VolleySingleton.getInstance(getContext()).getRequestQueue();
JSONParse();
calendarView.setEvents(events);
}
private void JSONParse() {
String url = "https://api.myjson.com/bins/14htbv";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
jsonArray = response.getJSONArray("info_stories");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String dateStr = hit.getString("date");
String text_main = hit.getString("text_main");

Date date = sdf.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
events.add(new EventDay(cal, R.drawable.ic_home));
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}, error -> {
error.printStackTrace();
});
mRequestQueue.add(request);
}

最佳答案

问题解决了)

public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
events = new ArrayList<>();
calendarView = (CalendarView) getView().findViewById(R.id.calendarView);
mRequestQueue = VolleySingleton.getInstance(getContext()).getRequestQueue();
JSONParse();
}
private void JSONParse() {
String url = "https://api.myjson.com/bins/14htbv";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
response -> {
try {
jsonArray = response.getJSONArray("info_stories");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject hit = jsonArray.getJSONObject(i);
String dateStr = hit.getString("date");
String text_main = hit.getString("text_main");

Date date = sdf.parse(dateStr);
Calendar cal = Calendar.getInstance();
cal.setTime(date);
events.add(new EventDay(cal, R.drawable.ic_home));
calendarView.setEvents(events);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}, error -> {
error.printStackTrace();
});
mRequestQueue.add(request);
}

关于java - 将事件添加到 "Material-Calendar-View"插件的日历时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56493192/

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