gpt4 book ai didi

android - 在日历中显示事件列表?

转载 作者:搜寻专家 更新时间:2023-11-01 07:45:57 27 4
gpt4 key购买 nike

我能够在日历下方的列表中显示事件,但我想通过 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

我的模拟器上的屏幕截图

enter image description here

编辑:

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/

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