我正在尝试使用GregorianCalendar对象填充ArrayList,以便我可以进一步将它附加到listview适配器。显示here的快照是我想要实现的......我希望日期对象列表成为组列表视图,以便它可以与特定日期下的事件进行比较(即事件将是儿童listview)。到目前为止,我已经编写了一些代码,但它并没有像snapshot那样使用日期来填充arraylist,而是仅添加当前日期(即只有一个元素)。提前致谢。
这是我的代码
public class EventFragment extends Fragment{
List<GregorianCalendar> dates = new ArrayList<GregorianCalendar>();
List<Events> events = new ArrayList<Events>();
SimpleDateFormat dateFormat;
GregorianCalendar calendar_date;
public EventFragment(){ }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_events, container, false);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
listView = (ListView) getView().findViewById(R.id.list);
dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
calendar_date = new GregorianCalendar();
dates.add(calendar_date);
for(int i = 0; i < dates.size(); i++){
Log.e("Date", ""+calendar_date.get(i));
}
}
}
答案 0 :(得分:0)
这实际上比我想象的要复杂得多。这并不难,但需要更多的代码。
我可以给出一个生成一系列日期的简单示例,如下所示。为了简单起见,我将只做一个月 - 2015年1月,例如......
Calendar startDate = new GregorianCalendar();
// Set the start date to 1st Jan 2015 and time to 00:00:00 using
// set(int year, int month, int day, int hourOfDay, int minute, int second)
// NOTE: the month field is in the range 0-11 with January being 0
startDate.set(2015, 0, 1, 0, 0, 0);
// Clone the start date and add one month to set the end date to
// 1st February 2015 00:00:00
Calendar endDate = startDate.clone();
endDate.add(Calendar.MONTH, 1); // This adds 1 month
// Step through each day from startDate to endDate (not including endDate itself)
while (startDate.before(endDate)) {
// Do whatever you need to do here to get the date string from startDate
// using SimpleDateFormat for example. For logging purposes you can
// use the next line...
Log.e("Date", startDate.toString());
// Now increment the day as follows...
startDate.add(Calendar.DAY_OF_MONTH, 1);
}
在保存事件数据时,您需要做更多的工作,我建议使用SQLite DB。然后,我建议您更改日期列表,只需保存格式化的日期字符串,而不是GregorianCalendar的实例。
List<String> dates = new ArrayList<String>();