- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我能够在日历下方的列表中显示事件,但我想通过 JSON 数据显示日历中的事件。我使用 CalendarView 来显示日历。我看过this但无法解决我真正想要的问题。
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#2E353D"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<ImageView
android:layout_width="30dp"
android:layout_height="match_parent"
android:padding="3dp"
android:src="@mipmap/calander" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"
android:text="Calendar"
android:textColor="#fff"
android:textSize="17dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<!-- <CalendarView
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" /> -->
<ListView
android:id="@+id/calenderlist"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
日历 fragment
public class CalenderFragment extends Fragment implements OnMonthChangedListener {
String Navigation_URL = "http://192.168.100.5:84/api/academics/getEvents";
String access_token;
ListView listView;
com.prolificinteractive.materialcalendarview.MaterialCalendarView calendarView;
ArrayList<CalenderPojoStudent> student_list_calender = new ArrayList<>();
private HashMap<Integer, List<Event>> map = new HashMap<>();
private List<CalendarDay> calevents = new ArrayList<>();
private Calendar cal;
private List<Event> eventList = new ArrayList<>();
private MyAdapterCalendar adapter;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calender, container, false);
getActivity().setTitle("");
setHasOptionsMenu(true);
calendarView = (com.prolificinteractive.materialcalendarview.MaterialCalendarView) view.findViewById(R.id.calendar);
final Calendar calendar = Calendar.getInstance();
calendarView.setSelectedDate(calendar.getTime());
calendarView.setSelectedDate(calendar.getTime());
listView = (ListView) view.findViewById(R.id.calenderlist);
adapter = new MyAdapterCalendar(getActivity(), eventList);
// listView.setAdapter(adapter);
// calendarView.addDecorator(new HighlightWeekendsDecorator());
calendarView.addDecorator(new OneDayDecorator());
// calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
// calendarView.addView(onMonthChanged(calendarView,date));
// calendarView.addDecorator(new EventDecorator(Color.RED, calendarDays));
//calendarView.addDecorator(new MySelectorDecorator(getActivity()));
// calendarView.addDecorator(new EventDecorator("#FF0000",student_list_calender));
// calendarView.addDecorator(new EventDecorator("",));
//calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
SessionManagement session = new SessionManagement(getContext());
session.checkLogin();
access_token = session.getAccesstToken();
makeJsonObjectRequest();
return view;
}
// List<Event> events = new ArrayList<>();
private void makeJsonObjectRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
String URL = Navigation_URL;
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
String Title = jsonObject.getString("Title");
try {
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ", "" + date);
CalendarDay day1 = CalendarDay.from(date);
Event event = new Event(date, Title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
if (!map.containsKey(month)) {
List<Event> events = new ArrayList<>();
events.add(event);
map.put(month, events);
} else {
List<Event> events = map.get(month);
events.add(event);
map.put(month, events);
}
calevents.add(day1);
} catch (ParseException e) {
e.printStackTrace();
}
}
//CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
//listView.setAdapter(calenderAdapter);
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
if (event != null && event.size() > 0)
adapter.addItems(event);
listView.setAdapter(adapter);
EventDecorator eventDecorator = new EventDecorator(Color.RED, calevents);
calendarView.addDecorator(eventDecorator);
} catch (JSONException e) {
makeText(getContext(), "Fetch failed!", LENGTH_SHORT).show();
e.printStackTrace();
}
//
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
makeText(getContext(), error.toString(), LENGTH_LONG).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "Bearer " + access_token);
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
/*
@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("id", master_id);
map.put("accessID", accessID);
map.put("currentUser", master_id);
return map;
} */
};
requestQueue.add(stringRequest);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.dashboard, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
// do s.th.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
// CalenderAdapter calenderAdapter = new CalenderAdapter(getActivity(), student_list_calender);
// listView.setAdapter(calenderAdapter);
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
if (event != null && event.size() > 0)
adapter.addItems(event);
widget.invalidateDecorators();
}
}
事件装饰器
public class EventDecorator implements DayViewDecorator {
/*
private final Drawable drawable;
private final CalendarDay day;
private final int color;
public EventDecorator(MaterialCalendarView view, Date date, int colors) {
// super(view);
this.day = CalendarDay.from(date);
this.color = colors;
this.drawable = createTintedDrawable(view.getContext(), color);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
if (this.day.equals(day)) {
return true;
}
return false;
}
@Override
public void decorate(DayViewFacade view) {
view.setSelectionDrawable(drawable);
}
private static Drawable createTintedDrawable(Context context, int color) {
return applyTint(createBaseDrawable(context), color);
}
private static Drawable applyTint(Drawable drawable, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(wrappedDrawable, color);
return wrappedDrawable;
}
private static Drawable createBaseDrawable(Context context) {
return ContextCompat.getDrawable(context, R.drawable.ic_menu_send);
}
*/
private int color;
private HashSet<CalendarDay> dates;
public EventDecorator(int color, Collection<CalendarDay> dates) {
this.color = color;
this.dates = new HashSet<>(dates);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}
@Override
public void decorate(DayViewFacade view) {
view.addSpan(new DotSpan(5, color));
}
}
事件
public class Event {
private Date date;
private String events;
public Event(Date date, String events) {
this.date = date;
this.events = events;
}
public Date getDate() {
return date;
}
public String getEvents() {
return events;
}
}
我的适配器日历
public class MyAdapterCalendar extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapterCalendar(Context context, List<Event> list) {
super(context, R.layout.activity_calendar_event, list);
this.mInflater = LayoutInflater.from(context);
this.list = list;
}
static class ViewHolder {
TextView text;
}
public void addItems(List<Event> list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.activity_calendar_event, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getEvents());
return convertView;
}
}
activity_calendar_event
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Hello World"
android:textSize="20sp"></TextView>
</RelativeLayout>
JSON
[
{
"EventID": 1011,
"Title": "May Day",
"Description": "Holiday for the International Labour Day",
"StartDate": "2017-05-01T00:00:00",
"StartTime": "00:00:00",
"EndDate": "2017-05-02T00:00:00",
"EndTime": "00:00:00",
"IsFullDay": true,
"IsHoliday": false,
"DateTime": null
},
{
"EventID": 1020,
"Title": "d",
"Description": "",
"StartDate": "2017-04-27T00:00:00",
"StartTime": "05:45:00",
"EndDate": "2017-04-27T00:00:00",
"EndTime": "05:45:00",
"IsFullDay": false,
"IsHoliday": false,
"DateTime": null
},
{
"EventID": 1019,
"Title": "Mother's Day",
"Description": "Mother's day",
"StartDate": "2017-04-26T00:00:00",
"StartTime": "10:00:00",
"EndDate": "2017-04-26T00:00:00",
"EndTime": "18:00:00",
"IsFullDay": false,
"IsHoliday": false,
"DateTime": null
}
]
I want to display the dot under certain date,which shows as event.And specific month should list specific month event rather than all the event ,which is the one i am getting on Currently.
最佳答案
您需要添加一个 DotSpan。此外,我还必须阅读图书馆文档,因为我自己还没有使用过它。
第 1 步:在您的 Fragment 类中
private List<CalendarDay> events = new ArrayList<>();
第 2 步:
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
// i copied your json to load form assets folder
// in our case you can get the json from the server
// or any other location of your choice
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
events.add(day);
}
} catch (Exception e) {
e.printStackTrace();
}
EventDecorator eventDecorator = new EventDecorator(Color.RED, events);
calendarView.addDecorator(eventDecorator);
}
第 3 步:更改您的 EventDecorator 类
public class EventDecorator implements DayViewDecorator {
private int color;
private HashSet<CalendarDay> dates;
public EventDecorator(int color, Collection<CalendarDay> dates) {
this.color = color;
this.dates = new HashSet<>(dates);
}
@Override
public boolean shouldDecorate(CalendarDay day) {
return dates.contains(day);
}
@Override
public void decorate(DayViewFacade view) {
view.addSpan(new DotSpan(5, color));
}
}
您可以查看 DotSpan 并自行自定义。它是一个 LinearBackgroundSpan
我的模拟器上的屏幕截图
编辑:
And specific month should list specific month event rather than all the event ,which is the one i am getting on Currently.
您需要检查当前显示的月份,并确保同步 ListView 以相应地显示事件。根据日历显示的月份清除列表中用于显示事件的列表数据并刷新列表。
或者您可以只在点击日期时显示当天的事件。
如何操作由您决定。你可以看看这个https://github.com/memfis19/Cadar应该提供洞察力或帮助您相应地修改代码的库。根据显示的月份,列表显示事件。
编辑 2:
我的适配器类
public class MyAdapter extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapter(Context context, List<Event> list) {
super(context, R.layout.row, list);
this.mInflater = LayoutInflater.from(context);
this.list = list;
}
static class ViewHolder {
TextView text;
}
public void addItems(List<Event> list) {
this.list.clear();
this.list.addAll(list);
notifyDataSetChanged();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, parent,false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.label);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getEvents());
return convertView;
}
}
我的日历 fragment
public class CalendarFragment extends Fragment implements OnMonthChangedListener {
private MaterialCalendarView calendarView;
private List<CalendarDay> calevents = new ArrayList<>();
private List<Event> eventList = new ArrayList<>();
private HashMap<Integer,List<Event>> map = new HashMap<>();
private ListView listView;
private MyAdapter adapter;
private Calendar cal;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.calendar, container, false);
listView = (ListView)view.findViewById(R.id.listview);
adapter = new MyAdapter(getActivity(),eventList);
listView.setAdapter(adapter);
calendarView = view.findViewById(R.id.calendarView);
calendarView.setDateTextAppearance(View.ACCESSIBILITY_LIVE_REGION_ASSERTIVE);
final Calendar calendar = Calendar.getInstance();
calendarView.setSelectedDate(calendar.getTime());
calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
@Override
public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
calendarView.setHeaderTextAppearance(R.style.AppTheme);
}
});
calendarView.setOnMonthChangedListener(this);
makeJsonObjectRequest();
return view;
}
private void makeJsonObjectRequest() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
String response = loadJSONFromAsset();
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
String StartDate = jsonObject.getString("StartDate");
Date date = simpleDateFormat.parse(StartDate);
String title = jsonObject.getString("Title");
Log.d("Date ",""+date);
CalendarDay day = CalendarDay.from(date);
Event event = new Event(date,title);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
if(!map.containsKey(month))
{
List<Event> events = new ArrayList<>();
events.add(event);
map.put(month,events);
}else
{
List<Event> events = map.get(month);
events.add(event);
map.put(month,events);
}
calevents.add(day);
}
} catch (Exception e) {
e.printStackTrace();
}
// after parsing
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
EventDecorator eventDecorator = new EventDecorator(Color.RED, calevents);
calendarView.addDecorator(eventDecorator);
}
public String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getActivity().getAssets().open("testjson.json");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
List<Event> event = map.get(month);
adapter.addItems(event);
}
}
关于android - 在日历中显示事件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44275601/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!