Android Open Source - Caldroid Caldroid Grid Adapter






From Project

Back to project page Caldroid.

License

The source code is released under:

MIT License

If you think the Android project Caldroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.roomorama.caldroid;
//w  ww.  j  a  v a2 s. c  om
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.caldroid.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import hirondelle.date4j.DateTime;

/**
 * The CaldroidGridAdapter provides customized view for the dates gridview
 *
 * @author thomasdao
 */
public class CaldroidGridAdapter extends BaseAdapter {
    protected ArrayList<DateTime> datetimeList;
    protected int month;
    protected int year;
    protected Context context;
    protected ArrayList<DateTime> disableDates;
    protected ArrayList<DateTime> selectedDates;

    // Use internally, to make the search for date faster instead of using
    // indexOf methods on ArrayList
    protected HashMap<DateTime, Integer> disableDatesMap = new HashMap<DateTime, Integer>();
    protected HashMap<DateTime, Integer> selectedDatesMap = new HashMap<DateTime, Integer>();

    protected DateTime minDateTime;
    protected DateTime maxDateTime;
    protected DateTime today;
    protected int startDayOfWeek;
    protected boolean sixWeeksInCalendar;
    protected boolean squareTextViewCell;
    protected Resources resources;

    /**
     * caldroidData belongs to Caldroid
     */
    protected HashMap<String, Object> caldroidData;
    /**
     * extraData belongs to client
     */
    protected HashMap<String, Object> extraData;

    public void setAdapterDateTime(DateTime dateTime) {
        this.month = dateTime.getMonth();
        this.year = dateTime.getYear();
        this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year,
                startDayOfWeek, sixWeeksInCalendar);
    }

    // GETTERS AND SETTERS
    public ArrayList<DateTime> getDatetimeList() {
        return datetimeList;
    }

    public DateTime getMinDateTime() {
        return minDateTime;
    }

    public void setMinDateTime(DateTime minDateTime) {
        this.minDateTime = minDateTime;
    }

    public DateTime getMaxDateTime() {
        return maxDateTime;
    }

    public void setMaxDateTime(DateTime maxDateTime) {
        this.maxDateTime = maxDateTime;
    }

    public ArrayList<DateTime> getDisableDates() {
        return disableDates;
    }

    public void setDisableDates(ArrayList<DateTime> disableDates) {
        this.disableDates = disableDates;
    }

    public ArrayList<DateTime> getSelectedDates() {
        return selectedDates;
    }

    public void setSelectedDates(ArrayList<DateTime> selectedDates) {
        this.selectedDates = selectedDates;
    }

    public HashMap<String, Object> getCaldroidData() {
        return caldroidData;
    }

    public void setCaldroidData(HashMap<String, Object> caldroidData) {
        this.caldroidData = caldroidData;

        // Reset parameters
        populateFromCaldroidData();
    }

    public HashMap<String, Object> getExtraData() {
        return extraData;
    }

    public void setExtraData(HashMap<String, Object> extraData) {
        this.extraData = extraData;
    }

    /**
     * Constructor
     *
     * @param context
     * @param month
     * @param year
     * @param caldroidData
     * @param extraData
     */
    public CaldroidGridAdapter(Context context, int month, int year,
                               HashMap<String, Object> caldroidData,
                               HashMap<String, Object> extraData) {
        super();
        this.month = month;
        this.year = year;
        this.context = context;
        this.caldroidData = caldroidData;
        this.extraData = extraData;
        this.resources = context.getResources();

        // Get data from caldroidData
        populateFromCaldroidData();
    }

    /**
     * Retrieve internal parameters from caldroid data
     */
    @SuppressWarnings("unchecked")
    private void populateFromCaldroidData() {
        disableDates = (ArrayList<DateTime>) caldroidData
                .get(CaldroidFragment.DISABLE_DATES);
        if (disableDates != null) {
            disableDatesMap.clear();
            for (DateTime dateTime : disableDates) {
                disableDatesMap.put(dateTime, 1);
            }
        }

        selectedDates = (ArrayList<DateTime>) caldroidData
                .get(CaldroidFragment.SELECTED_DATES);
        if (selectedDates != null) {
            selectedDatesMap.clear();
            for (DateTime dateTime : selectedDates) {
                selectedDatesMap.put(dateTime, 1);
            }
        }

        minDateTime = (DateTime) caldroidData
                .get(CaldroidFragment._MIN_DATE_TIME);
        maxDateTime = (DateTime) caldroidData
                .get(CaldroidFragment._MAX_DATE_TIME);
        startDayOfWeek = (Integer) caldroidData
                .get(CaldroidFragment.START_DAY_OF_WEEK);
        sixWeeksInCalendar = (Boolean) caldroidData
                .get(CaldroidFragment.SIX_WEEKS_IN_CALENDAR);
        squareTextViewCell = (Boolean) caldroidData
                .get(CaldroidFragment.SQUARE_TEXT_VIEW_CELL);

        this.datetimeList = CalendarHelper.getFullWeeks(this.month, this.year,
                startDayOfWeek, sixWeeksInCalendar);
    }

    public void updateToday() {
        today = CalendarHelper.convertDateToDateTime(new Date());
    }

    protected DateTime getToday() {
        if (today == null) {
            today = CalendarHelper.convertDateToDateTime(new Date());
        }
        return today;
    }

    @SuppressWarnings("unchecked")
    protected void setCustomResources(DateTime dateTime, View backgroundView,
                                      TextView textView) {
        // Set custom background resource
        HashMap<DateTime, Integer> backgroundForDateTimeMap = (HashMap<DateTime, Integer>) caldroidData
                .get(CaldroidFragment._BACKGROUND_FOR_DATETIME_MAP);
        if (backgroundForDateTimeMap != null) {
            // Get background resource for the dateTime
            Integer backgroundResource = backgroundForDateTimeMap.get(dateTime);

            // Set it
            if (backgroundResource != null) {
                backgroundView.setBackgroundResource(backgroundResource
                        .intValue());
            }
        }

        // Set custom text color
        HashMap<DateTime, Integer> textColorForDateTimeMap = (HashMap<DateTime, Integer>) caldroidData
                .get(CaldroidFragment._TEXT_COLOR_FOR_DATETIME_MAP);
        if (textColorForDateTimeMap != null) {
            // Get textColor for the dateTime
            Integer textColorResource = textColorForDateTimeMap.get(dateTime);

            // Set it
            if (textColorResource != null) {
                textView.setTextColor(resources.getColor(textColorResource
                        .intValue()));
            }
        }
    }

    /**
     * Customize colors of text and background based on states of the cell
     * (disabled, active, selected, etc)
     * <p/>
     * To be used only in getView method
     *
     * @param position
     * @param cellView
     */
    protected void customizeTextView(int position, TextView cellView) {
        cellView.setTextColor(Color.BLACK);

        // Get the padding of cell so that it can be restored later
        int topPadding = cellView.getPaddingTop();
        int leftPadding = cellView.getPaddingLeft();
        int bottomPadding = cellView.getPaddingBottom();
        int rightPadding = cellView.getPaddingRight();

        // Get dateTime of this cell
        DateTime dateTime = this.datetimeList.get(position);

        // Set color of the dates in previous / next month
        if (dateTime.getMonth() != month) {
            cellView.setTextColor(resources
                    .getColor(R.color.caldroid_darker_gray));
        }

        boolean shouldResetDiabledView = false;
        boolean shouldResetSelectedView = false;

        // Customize for disabled dates and date outside min/max dates
        if ((minDateTime != null && dateTime.lt(minDateTime))
                || (maxDateTime != null && dateTime.gt(maxDateTime))
                || (disableDates != null && disableDatesMap
                .containsKey(dateTime))) {

            cellView.setTextColor(CaldroidFragment.disabledTextColor);
            if (CaldroidFragment.disabledBackgroundDrawable == -1) {
                cellView.setBackgroundResource(R.drawable.disable_cell);
            } else {
                cellView.setBackgroundResource(CaldroidFragment.disabledBackgroundDrawable);
            }

            if (dateTime.equals(getToday())) {
                cellView.setBackgroundResource(R.drawable.red_border_gray_bg);
            }
        } else {
            shouldResetDiabledView = true;
        }

        // Customize for selected dates
        if (selectedDates != null && selectedDatesMap.containsKey(dateTime)) {
            if (CaldroidFragment.selectedBackgroundDrawable != -1) {
                cellView.setBackgroundResource(CaldroidFragment.selectedBackgroundDrawable);
            } else {
                cellView.setBackgroundColor(resources
                        .getColor(R.color.caldroid_sky_blue));
            }

            cellView.setTextColor(CaldroidFragment.selectedTextColor);
        } else {
            shouldResetSelectedView = true;
        }

        if (shouldResetDiabledView && shouldResetSelectedView) {
            // Customize for today
            if (dateTime.equals(getToday())) {
                cellView.setBackgroundResource(R.drawable.red_border);
            } else {
                cellView.setBackgroundResource(R.drawable.cell_bg);
            }
        }

        // Set text
        cellView.setText("" + dateTime.getDay());

        // Set custom color if required
        setCustomResources(dateTime, cellView, cellView);

        // Somehow after setBackgroundResource, the padding collapse.
        // This is to recover the padding
        cellView.setPadding(leftPadding, topPadding, rightPadding,
                bottomPadding);
    }

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

    @Override
    public Object getItem(int position) {
        return datetimeList.get(position);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        TextView cellView = (TextView) convertView;

        // For reuse
        if (convertView == null) {
            if (squareTextViewCell) {
                cellView = (TextView) inflater.inflate(R.layout.square_date_cell, null);
            } else {
                cellView = (TextView) inflater.inflate(R.layout.normal_date_cell, null);
            }
        }

        customizeTextView(position, cellView);

        return cellView;
    }

}




Java Source Code List

com.antonyt.infiniteviewpager.InfinitePagerAdapter.java
com.antonyt.infiniteviewpager.InfiniteViewPager.java
com.caldroidsample.CaldroidSampleActivity.java
com.caldroidsample.CaldroidSampleCustomAdapter.java
com.caldroidsample.CaldroidSampleCustomFragment.java
com.roomorama.caldroid.CaldroidFragment.java
com.roomorama.caldroid.CaldroidGridAdapter.java
com.roomorama.caldroid.CaldroidListener.java
com.roomorama.caldroid.CalendarHelper.java
com.roomorama.caldroid.DateGridFragment.java
com.roomorama.caldroid.MonthPagerAdapter.java
com.roomorama.caldroid.SquareTextView.java
com.roomorama.caldroid.WeekdayArrayAdapter.java