简体   繁体   English

如何在ListView Android上显示一周内的日期

[英]How to display date in a week on ListView Android

I have this: 我有这个:

Date date = new Date();
SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 
String dateStr = curFormater.format(date);

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String [] {dateStr}));

But it only displays once, I want to make it into listview. 但它只显示一次,我想把它变成listview。 Aany ideas? 很多想法? Thx. 谢谢。

I'm not sure why you would want a list view of just one date, however, your problem seems to be the understanding of using an ListView, for example your array has one item, as in my first example 我不确定为什么你会想要一个日期的列表视图,但是,你的问题似乎是使用ListView的理解,例如你的数组有一个项目,如我的第一个例子

String[] = {"hello"};

This will just show one item in the list view, as in your example 这将只显示列表视图中的一个项目,如您的示例所示

String[] = {"hello","how","are","you"};

Will show 4 lines, if you want to show the same date multiple times, you could possibly create an array like 将显示4行,如果你想多次显示相同的日期,你可能会创建一个像这样的数组

int x = 5;

String[] print = new String[x];

for(int i = 0; i<5; i++) {
   print[i] = date;
}

This will print out 5 lines of date 这将打印出5行日期

Try something like this... 尝试这样的事......

SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy"); 
GregorianCalendar date = new GregorianCalendar();
String[] dateStringArray = new String[7];

for (int day = 0; day < 7; day++) {
    dateStringArray[day] = curFormater.format(date.getTime());
    date.roll(Calendar.DAY_OF_YEAR, true);
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dateStringArray));

The array contains just one item: 该数组只包含一个项目:

...new String [] {dateStr}...

You can easily test the result doing: 您可以轻松地测试结果:

...new String [] {dateStr,dateStr,dateStr,dateStr,dateS}...

then build the array with the dates you want to show. 然后使用要显示的日期构建数组。

You should really try to be a bit more descriptive and elaborate on what it's really that you're after. 你应该尝试更具描述性,并详细说明你所追求的是什么。 I assume that by now you've figured out how to add multiple entries to a list, based on the answers already given. 我假设到现在为止你已经找到了如何根据已经给出的答案向列表中添加多个条目。

To fill your list with subsequent dates, simply get a Calendar instance and create a loop incrementing the day field by 'one' with every step. 要使用后续日期填充列表,只需获取一个日历实例,并创建一个循环,将每个步骤的日期字段递增“一”。 Something like: 就像是:

List<String> dateList = new ArrayList<String>();
Calendar calendar = Calendar.getInstance();
for (int i=0; i<31; i++) {
    calendar.add(Calendar.DATE, 1);
    SimpleDateFormat curFormater = new SimpleDateFormat("EEEE, MMMM dd, yyyy");
    dateList.add(curFormater.format(calendar.getTime()));
}
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dateList));

That should show you today plus the next 31 days in a list. 这应该会在今天向您显示列表中的下一个31天。

//edit: yuck, missed MisterSquonk's reply, which covers a similar approach... //编辑:哎呀,错过了MisterSquonk的回复,它涵盖了类似的方法......

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

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