开发者

how to populate a gridview for a calendar?

i have a 7x6 grid.here i have to populate the calendar for the selected month. i have dat开发者_StackOverflow社区e,month and year.with the help of these value is it possible to populate my grid view with the help of any algorithm? same like this

how to populate a gridview for a calendar?


I would say utilize the java "GregorianCalendar" class:

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

I wrote a simple java program to demonstrate how you would populate it:

    //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();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜