【发布时间】:2014-12-04 21:01:32
【问题描述】:
我正在尝试使用 GregorianCalendar 对象填充 ArrayList,以便我可以进一步将其附加到 listview 适配器。 here 显示的快照是我想要实现的......我希望日期对象列表成为组列表视图,以便它可以与特定日期下的事件进行比较(即事件将是子列表视图)。到目前为止,我已经编写了一些代码,但它并没有像snapshot 那样用日期填充数组列表,而是只添加了当前日期(即只有一个元素)。提前致谢。
这是我的代码
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));
}
}
}
【问题讨论】:
-
您只添加到列表一次 (
dates.add(calendar_date);)。 -
@GriffeyDog,我将 (dates.add(calendar_date);) 移动到循环中,它仍然返回与今天相关的日期
-
@mish :
GregorianCalendar只能代表一个日期/时间。它不像日历应用那样具有多个日期的“完整”日历。 -
我很难说出你的代码的对象是什么,所以我无法真正提供解决方案。将
dates.add()放在现有循环中根本没有帮助,因为该循环基于您尝试添加的列表 (dates.size) 的大小。 -
@Squonk,请问我应该使用其他什么来代替 GregorianCalendar,
标签: java android listview arraylist