如何在自定义日历适配器中设置周日

时间:2015-12-06 12:40:14

标签: android

我正在使用自定义Base Adapter制作自定义日历。压延机运行良好。但我的问题是无法将日历放在calander中。我想在例如(周日,周一,周二......星期六)插入所有七天。压延机。这是我的代码

   // Inner Class Adapter
public class GridCellAdapter extends BaseAdapter {
    private static final String tag = "GridCellAdapter";
    private final Context _context;

    private final List<String> list;
    private static final int DAY_OFFSET = 1;
    private final String[] weekdays = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    private final int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    private int daysInMonth;
    private int currentDayOfMonth;
    private int currentWeekDay;
    private final HashMap<String, Integer> eventsPerMonthMap;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");


    // Days in Current Month
    public GridCellAdapter(Context context, int textViewResourceId, int month, int year) {
        super();
        this._context = context;
        this.list = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));

        // Print Month
        printMonth(month, year);

        // Find Number of Events
        eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
    }

    private String getMonthAsString(int i) {
        return months[i];
    }

    private String getWeekDayAsString(int i) {
        return weekdays[i];
    }

    private int getNumberOfDaysOfMonth(int i) {
        return daysOfMonth[i];
    }

    public String getItem(int position) {
        return list.get(position);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * Prints Month
     *
     * @param mm
     * @param yy
     */
    private void printMonth(int mm, int yy) {
        Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
        int trailingSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
        String currentMonthName = getMonthAsString(currentMonth);
        daysInMonth = getNumberOfDaysOfMonth(currentMonth);


        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;

        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 1;

        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);

        }

        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        trailingSpaces = currentWeekDay;

        if (cal.isLeapYear(cal.get(Calendar.YEAR)))
            if (mm == 2)
                ++daysInMonth;
            else if (mm == 3)
                ++daysInPrevMonth;

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {

            list.add(String.valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET) + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);
        }

        // Current Month Days
        for (int i = 1; i <= daysInMonth; i++) {
            Log.d(currentMonthName, String.valueOf(i) + " " + getMonthAsString(currentMonth) + " " + yy);
            if (i == getCurrentDayOfMonth()) {
                list.add(String.valueOf(i) + "-BLUE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
            } else {
                list.add(String.valueOf(i) + "-WHITE" + "-" + getMonthAsString(currentMonth) + "-" + yy);
            }
        }

        // Leading Month days
        for (int i = 0; i < list.size() % 7; i++) {
            Log.d(tag, "NEXT MONTH:= " + getMonthAsString(nextMonth));
            list.add(String.valueOf(i + 1) + "-GREY" + "-" + getMonthAsString(nextMonth) + "-" + nextYear);
        }
    }

1 个答案:

答案 0 :(得分:0)

您更改了自定义适配器的layout。我有同样的问题然后我解决它像下面的代码

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@color/text_yellowish"
        android:orientation="horizontal"
        android:layout_marginRight="2dp"
        android:weightSum="7">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"

            android:weightSum="2">

            <TextView
                android:id="@+id/text1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الأحد"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="SUN"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الإثنين"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text4"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="MON"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text5"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الثلاثاء"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text6"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="TUE"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text7"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الأربعاء"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text8"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="WED"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text9"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الخميس"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text10"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="THU"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_marginRight="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text11"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="الجمعة"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text12"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="FRI"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="1dp"
            android:layout_weight="1"
            android:background="@color/white"
            android:orientation="vertical"
            android:weightSum="2">

            <TextView
                android:id="@+id/text13"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="يوم السبت"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />

            <TextView
                android:id="@+id/text14"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="SAT"
                android:textColor="@color/text_yellowish"
                android:textSize="15sp" />
        </LinearLayout>

    </LinearLayout>