Android:有活动的日历 [英] Android: Calendar with Events
问题描述
我在Android中有关于日历的问题。我想添加数据库上的事件。我基本上想要的是当我打开我的日历片段时,它将调用Web服务并从服务器中获取数据,其中包括日期和相应的事件,当我在日历中点击该日期时,显示指定日期的事件。我遇到的问题是:
I have some issue regarding calendar in android. I want to add events which is on database. What I basically want is when i open my calendar fragment it will call web service and fetch the data from the server which includes dates and their respective events when i click on that date in calendar it shows me the events on specified date. Issues which i faced are:
-
它在此行上显示一些错误
date = sdf.parse(日期);
,它说
java.lang.NullPointerException
at java.text.SimpleDateFormat.parse(SimpleDateFormat.java:1009)
at java.text.DateFormat.parse(DateFormat.java:553)
at com.example.aksystems.practiceportal.Calendar.showJSON(Calendar.java:118)
成功运行前两天,问题是会生成点日历日两次。示例:我的服务器响应是
Two days before it runs successfully and the issue is it will generate point on calendar day twice. Example: my server response is
[{"d":"a","events":"2017\/04\/13 18:20:43"},{"d":"a","events":"2017\/04\/10 18:20:40"}]`
所以它会指出日期13一次和日期10这么多次为什么?
so it will point date 13 once and date 10 so many times why?
Calendar.java
Calendar.java
public class Calendar extends Fragment {
CalendarView calendarView;
private EditText editTextId;
private Button buttonGet;
private TextView textViewResult;
String dates,events;
private ProgressDialog loading;
CompactCalendarView compactCalendarView;
String id = "a";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = null;
public Calendar() {}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_layout_calendar, container, false);
compactCalendarView = (CompactCalendarView) view.findViewById(R.id.compactcalendar_view);
editTextId = (EditText) view.findViewById(R.id.editTextId);
buttonGet = (Button)view. findViewById(R.id.buttonGet);
textViewResult = (TextView) view.findViewById(R.id.textViewResult);
getData();
return view;
}
private void getData() {
loading = ProgressDialog.show(getActivity(),"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+"?e="+id;
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getActivity(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
requestQueue.add(stringRequest);
}
private void showJSON(String response)
{
for (int i=0;i < response.length();i++)
{
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(i);
dates = collegeData.getString(Config.KEY_DATES);
events = collegeData.getString(Config.KEY_EVENTS);
//vc = collegeData.getString(Config.KEY_VC);*/
} catch (JSONException e) {
e.printStackTrace();
}
// textViewResult.setText(""+dates);
// String myDate = "2017/03/30 18:10:45";
try
{
date = sdf.parse(dates);
} catch (ParseException e) {
e.printStackTrace();
}
// Convert Date into milliseconds
assert date != null;
long millis = date.getTime();
Event ev1 = new Event(Color.RED, millis, events);
compactCalendarView.addEvent(ev1);
/* List<Event> events = compactCalendarView.getEvents(millis);
Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();*/
compactCalendarView.setListener(new CompactCalendarView.CompactCalendarViewListener()
{
@Override
public void onDayClick(Date dateClicked) {
List<Event> events = compactCalendarView.getEvents(dateClicked);
// Log.d(TAG, "Day was clicked: " + dateClicked + " with events " + events);
Toast.makeText(getActivity(), "" + events, Toast.LENGTH_SHORT).show();
}
@Override
public void onMonthScroll(Date firstDayOfNewMonth)
{
// Log.d(TAG, "Month was scrolled to: " + firstDayOfNewMonth);
// Toast.makeText(getActivity(), "Month was scrolled to: " + firstDayOfNewMonth, Toast.LENGTH_SHORT).show();
}
});
}
}
}
推荐答案
- 这是因为您传递的日期不是当前格式。看到你的PHP脚本。
- 你的变量
response.length()
返回值超过两个,所以它将指向多个事件在最后一天。尝试使用JSONArray.size()
来查找响应的长度。
- This is because your passed date is not in current format. See your PHP script.
- Your varible
response.length()
returns value more than two so it will point to more than one events in last date. Try usingJSONArray.size()
to find the length of response.
这篇关于Android:有活动的日历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!