如何在Android中显示日历视图?

时间:2013-05-23 08:28:53

标签: android android-view android-calendar

在我的应用程序中,我需要在其上显示日历和一些事件,如下图所示。 enter image description here

我已阅读this主题,但其中描述的libs不符合我的需求。我也发现了这个lib:Extended Calendar View,但它只适用于api等级为14或更高的设备,而我需要支持api等级10。 也许有人知道一些图书馆来解决这个问题?任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

我引用此链接创建类似link

的内容
/**
             * NOTE: YOU NEED TO IMPLEMENT THIS PART Given the YEAR, MONTH, retrieve
             * ALL entries from a SQLite database for that month. Iterate over the
             * List of All entries, and get the dateCreated, which is converted into
             * day.
             * 
             * @param year
             * @param month
             * @return
             */
            private HashMap findNumberOfEventsPerMonth(int year, int month)
                {
                    HashMap map = new HashMap<String, Integer>();
                    // DateFormat dateFormatter2 = new DateFormat();
                    //                      
                    // String day = dateFormatter2.format("dd", dateCreated).toString();
                    //
                    // if (map.containsKey(day))
                    // {
                    // Integer val = (Integer) map.get(day) + 1;
                    // map.put(day, val);
                    // }
                    // else
                    // {
                    // map.put(day, 1);
                    // }
                    return map;
                }

在上面的代码中创建包含日期的Hashmap。并使用此条件替换if条件(根据您的需要进行更改)

if ((mEventsPerMonthMap != null) && (!mEventsPerMonthMap.isEmpty())) {
        Set<String> keys = mEventsPerMonthMap.keySet();
        for (String key : keys) {
            if (key.equals(theday + "/" + monthInNo+ "/" + theyear)
            && mEventsPerMonthMap.containsKey((String.format("%02d", Integer.parseInt(theday)))
            + "/"+ monthInNo+ "/" + theyear)) {
datewiseEventmap.put(theday + "/" + monthInNo+"/" + theyear,
                        (ArrayList<Appointment>)
                        mEventsPerMonthMap.get((String.format("%02d",Integer.parseInt(theday)))+ "/" + monthInNo+ "/" + theyear));}}

更改每个网格单元格

if (datewiseEventmap != null && datewiseEventmap.size() > 0) {
            mNumEvents = (ArrayList<Appointment>) datewiseEventmap
                    .get(theday + "/" + monthInNo+ "/" + theyear);


            eventName.setText(sbf);


            eventName.setBackgroundResource(R.drawable.appointment_name_bg);
            //gridcell.setBackgroundResource(R.drawable.calender_details_description);
            eventCount.setVisibility(View.VISIBLE);
            //eventCount.setBackgroundResource(R.drawable.calender_details_description);
            if (mNumEvents.size() > 1) {
                eventCount.setVisibility(View.VISIBLE);
                eventCount.setText("+ " + String.valueOf(mNumEvents.size()));

            }