【问题标题】:how to populate a gridview for a calendar?如何填充日历的gridview?
【发布时间】:2011-05-15 06:02:02
【问题描述】:

我有一个 7x6 的网格。在这里我必须填充所选月份的日历。 我有日期、月份和年份。在这些值的帮助下,是否可以借助任何算法来填充我的网格视图?像这样

【问题讨论】:

  • @Gilbert: java 类中是否有任何算法或方法可以做到这一点?让我知道这是怎么可能的

标签: android algorithm gridview calendar populate


【解决方案1】:

我会说利用 java "GregorianCalendar" 类:

http://developer.android.com/reference/java/util/GregorianCalendar.html

我编写了一个简单的 java 程序来演示如何填充它:

    //calendar for November 1986
    GregorianCalendar gCal = new GregorianCalendar(1986, Calendar.NOVEMBER, 1);
    //this gets the day of week range 1-7, Sunday - Saturday
    int currentDay = gCal.get(Calendar.DAY_OF_WEEK);
    //backtracks to the beginning of current week (Sunday)
    gCal.add(Calendar.DAY_OF_YEAR, Calendar.SUNDAY - currentDay);

    int gridSizeX = 7, gridSizeY = 6;
    for (int i = 0; i < gridSizeY; i++)
    {
        for (int j = 0; j < gridSizeX; j++)
        {
            //fill in your cell with this value
            System.out.print(gCal.get(Calendar.DAY_OF_MONTH));
            System.out.print(" ");

            //add one to the day and keep going
            gCal.add(Calendar.DAY_OF_YEAR, 1);
        }
        System.out.println();
    }

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 2012-01-12
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多