我有Calendar Widget,下面有ListView来填充相关月份的事件。列表显示在特定月份。但日历上有一个问题,即2017年6月有 这些活动也会在2015,2016,2018展出...........这个问题怎么解决?
MyAdapterCalendar
public class MyAdapterCalendar extends ArrayAdapter<Event> {
private List<Event> list;
private LayoutInflater mInflater;
public MyAdapterCalendar(Context context, List<Event> list) {
super(context, R.layout.calender_student_listitems, list);
this.mInflater = LayoutInflater.from(context);
this.list = list;
}
public void clearItems(List<Event> list) {
this.list.clear();
// this.list.removeAll(list);
notifyDataSetChanged();
}
static class ViewHolder {
TextView text;
TextView student_calender_date;
}
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.calender_student_listitems, parent, false);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView.findViewById(R.id.student_calender_events);
viewHolder.student_calender_date = (TextView) convertView.findViewById(R.id.student_calender_date);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setText(list.get(position).getEvents());
viewHolder.student_calender_date.setText(list.get(position).getRealdate());
return convertView;
}
}
CalenderFragment
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").substring(0, 10);
String Title = jsonObject.getString("Title");
try {
Date date = simpleDateFormat.parse(StartDate);
Log.d("Date ", "" + date);
CalendarDay day1 = CalendarDay.from(date);
System.out.println("DAY1" + day1);
Event event = new Event(date, Title, StartDate);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
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);
//Collections.reverse(events);
//rever
}
calevents.add(day1);
} catch (ParseException e) {
e.printStackTrace();
}
}
cal = Calendar.getInstance();
int month = cal.get(Calendar.MONTH);
// int year=cal.get(Calendar.YEAR);
List<Event> event = map.get(month);
if (event != null && event.size() > 0)
adapter.addItems(event);
//Collections.reverse(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
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
List<Event> event = map.get(month);
List<Event> event1 = map.get(year);
if (event != null && event.size() > 0) {
adapter.addItems(event);
// adapter.clearItems(event);
System.out.println("Adapter" + adapter);
} else {
adapter.clearItems(event);
// event.clear();
}
/* if (event1 != null && event1.size() > 0)
adapter.addItems(event1);
else
adapter.clearItems(event1);
*/
widget.invalidateDecorators();
}
如何在日期而不是特定日期清除列表?需要帮助
答案 0 :(得分:1)
尝试添加此内容,创建hashmap以获取年份
private HashMap<Integer, List<Event>> map = new HashMap<>();
private HashMap<Integer, String> map1 = new HashMap();
try {
Date date = simpleDateFormat.parse(StartDate);
// Log.d("Date ", "" + date);
CalendarDay day1 = CalendarDay.from(date);
Event event = new Event(date, Title, StartDate);
cal = Calendar.getInstance();
cal.setTime(date);
int month = cal.get(Calendar.MONTH);
System.out.println("MOnth" + month);
int year = cal.get(Calendar.YEAR);
map1.put(year, "");
System.out.println("map1" + map);
if ((!map.containsKey(month)) && (!map.containsKey(year))) {
List<Event> events = new ArrayList<>();
events.add(event);
map.put(month, events);
map1.put(year, "");
System.out.println("Year" + year);
} else {
List<Event> events = map.get(month);
events.add(event);
map.put(month, events);
map1.put(year, "");
System.out.println("Year" + year);
//Collections.reverse(events);
//rever
}
calevents.add(day1);
} catch (ParseException e) {
e.printStackTrace();
}
@Override
public void onMonthChanged(MaterialCalendarView widget, CalendarDay date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date.getDate());
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
List<Event> event = map.get(month);
String event1=map1.get(year);
// List<Event> event1 = map1.get(year);
System.out.println("OUT" + (event != null && event.size() > 0));
if ((event != null && event.size() > 0) && (event1 != null )) {
adapter.addItems(event);
System.out.println("Adapter" + adapter);
} else {
adapter.clearItems(event);
// event.clear();
}
widget.invalidateDecorators();
}