简体   繁体   English

用GregorianCalendar日期对象填充ArrayList

[英]Populating ArrayList with GregorianCalendar date objects

I am trying to populate an ArrayList with a GregorianCalendar object so that i can further attach it to a listview adapter. 我试图用GregorianCalendar对象填充ArrayList,以便我可以进一步将其附加到listview适配器。 The snapshot shown here is what i want to achieve.....i want the list of date object to be the group listview so that it can compared against events the fall under a particular day (ie events will be the child listview). 此处显示的快照是我想要实现的.....我希望日期对象的列表成为组listview,以便可以将其与特定日期下的事件进行比较(即事件将是子listview)。 So far i have written some code but it doesn't populate the arraylist with dates like in the snapshot , instead it adds only the current date (ie only one element). 到目前为止,我已经编写了一些代码,但是它没有像快照中那样用日期填充arraylist,而是仅添加了当前日期(即仅添加了一个元素)。 Thanks in advance. 提前致谢。

Here is my code 这是我的代码

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));
    }       
}
}

This is actually more complicated than I think you realise. 这实际上比我想像的要复杂。 It's not difficult but will take more code. 这并不困难,但是会花费更多的代码。

I can give a simple example of generating a range of dates as follows. 我可以举一个生成日期范围的简单示例,如下所示。 To keep things simple I'll just do one month - January 2015, for example... 为简单起见,例如,我只做一个月-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);
}

You'll need to do a lot more work when it comes to saving event data and I'd suggest using a SQLite DB. 保存事件数据时,您需要做更多的工作,我建议您使用SQLite DB。 I'd then suggest you change your dates list to simply hold the formatted date strings instead of instances of GregorianCalendar. 然后,我建议您将日期列表更改为仅保留格式化的日期字符串,而不要使用GregorianCalendar实例。

List<String> dates = new ArrayList<String>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM