Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/207.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何突出显示日历日期哪些日期来自数据库_Android_Calendar - Fatal编程技术网

Android 如何突出显示日历日期哪些日期来自数据库

Android 如何突出显示日历日期哪些日期来自数据库,android,calendar,Android,Calendar,我有一个日期数据库,其中我有多个日期,所以我想突出显示我从数据库中获得的所有日期的背景我尝试了一些逻辑,但没有成功我在这里把我的代码放在无限循环中。我想输出类似这样的内容假设我在数据库中有2013年3月5日和2013年3月9日这样的日期,那么两个日期背景应该在日历视图中突出显示。我使用了这个链接,这里是我的代码: 在printmount()函数中,我做了一些更改 public class CalendarActivity extends Activity implements OnClickLi

我有一个日期数据库,其中我有多个日期,所以我想突出显示我从数据库中获得的所有日期的背景我尝试了一些逻辑,但没有成功我在这里把我的代码放在无限循环中。我想输出类似这样的内容假设我在数据库中有2013年3月5日和2013年3月9日这样的日期,那么两个日期背景应该在日历视图中突出显示。我使用了这个链接,这里是我的代码:

在printmount()函数中,我做了一些更改

public class CalendarActivity extends Activity implements OnClickListener {
private static final String tag = "CalendarActivity";
private TextView currentMonth;
private Button selectedDayMonthYearButton;
private ImageView prevMonth;
private ImageView nextMonth;
private GridView calendarView;
private GridCellAdapter adapter;
private Calendar _calendar;
@SuppressLint("NewApi")
private int month, year;

@SuppressLint({ "NewApi", "NewApi", "NewApi", "NewApi" })
private final DateFormat dateFormatter = new DateFormat();
private static final String dateTemplate = "MMMM yyyy";
int i;
String valuofday;
int stryear;
DB_Database db;
String strmonthvalueofdatabase;
String[] strdate;
String value;
ArrayList<String> arraylist = new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_calendar_view);
    db = new DB_Database(this);

    _calendar = Calendar.getInstance(Locale.getDefault());
    month = _calendar.get(Calendar.MONTH) + 1;
    year = _calendar.get(Calendar.YEAR);
    // Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " +
    // "Year: "
    // + year);

    selectedDayMonthYearButton = (Button) this
            .findViewById(R.id.selectedDayMonthYear);
    selectedDayMonthYearButton.setText("Selected: ");

    prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
    prevMonth.setOnClickListener(this);

    currentMonth = (TextView) this.findViewById(R.id.currentMonth);
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));

    nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
    nextMonth.setOnClickListener(this);

    calendarView = (GridView) this.findViewById(R.id.calendar);

    // Initialised
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

/**
 * 
 * @param month
 * @param year
 */
private void setGridCellAdapterToDate(int month, int year) {
    adapter = new GridCellAdapter(getApplicationContext(),
            R.id.calendar_day_gridcell, month, year);
    _calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
    currentMonth.setText(DateFormat.format(dateTemplate,
            _calendar.getTime()));
    adapter.notifyDataSetChanged();
    calendarView.setAdapter(adapter);
}

@Override
public void onClick(View v) {
    if (v == prevMonth) {
        if (month <= 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        // Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
        // + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }
    if (v == nextMonth) {
        if (month > 11) {
            month = 1;
            year++;
        } else {
            month++;
        }
        // Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
        // + month + " Year: " + year);
        setGridCellAdapterToDate(month, year);
    }

}

@Override
public void onDestroy() {
    // Log.d(tag, "Destroying View ...");
    super.onDestroy();
}

// Inner Class
public class GridCellAdapter extends BaseAdapter implements OnClickListener {
    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 String[] months = { "January", "February", "March",
            "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };
    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 Button gridcell;
    private TextView num_events_per_day;
    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>();
        // Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
        // + "Year: " + year);
        Calendar calendar = Calendar.getInstance();
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
        stryear = calendar.get(Calendar.YEAR);
        // Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
        // Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
        // Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());
        printMonth(month, year);

        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);
        // The number of days to leave blank at
        // the start of this month.

        int trailingSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

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

        // Log.d(tag, "Current Month: " + " " + currentMonthName +
        // " having "
        // + daysInMonth + " days.");

        // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
        GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
        // Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());

        if (currentMonth == 11) {
            prevMonth = currentMonth - 1;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 0;
            prevYear = yy;
            nextYear = yy + 1;
            // Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
            // + prevMonth + " NextMonth: " + nextMonth
            // + " NextYear: " + nextYear);
        } else if (currentMonth == 0) {
            prevMonth = 11;
            prevYear = yy - 1;
            nextYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            nextMonth = 1;
            // Log.d(tag, "**--> PrevYear: " + prevYear + " PrevMonth:"
            // + prevMonth + " NextMonth: " + nextMonth
            // + " NextYear: " + nextYear);
        } else {
            prevMonth = currentMonth - 1;
            nextMonth = currentMonth + 1;
            nextYear = yy;
            prevYear = yy;
            daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
            // Log.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
            // + prevMonth + " NextMonth: " + nextMonth
            // + " NextYear: " + nextYear);
        }

        // Compute how much to leave before before the first day of the
        // month.
        // getDay() returns 0 for Sunday.
        int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
        String strcurrentweekdat = Integer.toString(currentWeekDay);
        Log.v("currentweekdays", strcurrentweekdat);
        trailingSpaces = currentWeekDay;

        // Log.d(tag, "Week Day:" + currentWeekDay + " is "
        // + getWeekDayAsString(currentWeekDay));
        // Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
        // Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);

        if (cal.isLeapYear(cal.get(Calendar.YEAR)) && mm == 1) {
            ++daysInMonth;
        }

        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
            // Log.d(tag,
            // "PREV MONTH:= "
            // + prevMonth
            // + " => "
            // + getMonthAsString(prevMonth)
            // + " "
            // + String.valueOf((daysInPrevMonth
            // - trailingSpaces + DAY_OFFSET)
            // + i));
            list.add(String
                    .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                            + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);

        }
        String strcurrentmonth = getMonthAsString(currentMonth);
        Log.v("strcurrentmonth", strcurrentmonth);

        String strcurrentyear = Integer.toString(stryear);
        Log.v("strcurrentmonth", strcurrentyear);

        // for database file

        SQLiteDatabase sqlitedatabase = db.getReadableDatabase();
        Cursor cursor = sqlitedatabase.query(DB_Database.REPORT, null,
                null, null, null, null, null);

        int hilihgtedDate = 0;

        // get array of hilighted dates...
        ArrayList<String> arraylist = new ArrayList<String>();

        // get array of hilighted dates...
        cursor.moveToFirst();
        do {
            arraylist.add(cursor.getString(cursor
                    .getColumnIndex(DB_Database.REPORT_MEDICINE_DATE)));
            hilihgtedDate++;
        } while (cursor.moveToNext() == true);

        // now that you have the dates to be hilighted, create the month
        // view as you were doing previously,
        // Trailing Month days
        for (int i = 0; i < trailingSpaces; i++) {
            Log.d(tag,
                    "PREV MONTH:= "
                            + prevMonth
                            + " => "
                            + getMonthAsString(prevMonth)
                            + " "
                            + String.valueOf((daysInPrevMonth
                                    - trailingSpaces + DAY_OFFSET)
                                    + i));
            list.add(String
                    .valueOf((daysInPrevMonth - trailingSpaces + DAY_OFFSET)
                            + i)
                    + "-GREY"
                    + "-"
                    + getMonthAsString(prevMonth)
                    + "-"
                    + prevYear);
        }

        int k = 0;
        // Current Month Days
        for (i = 1; i <= daysInMonth; i++) {

            // prevents un-ncesessart splitting steps...
            if (k <= hilihgtedDate) {
                /*
                 * checks if current date is equal to 1st hilighted date...
                 * if yes, then hilight the date and get the next hilihgted
                 * date, if no, then just add that date
                 */
                Log.v("string arraylist", arraylist.get(i));
                String[] splitstrdate = arraylist.get(k).split("/");

                String splitstrday = splitstrdate[0];
                Log.v("splitstrday", splitstrday);
                int intsplitstrday = Integer.parseInt(splitstrday);

                String splitstrmonth = splitstrdate[1];
                int intmonth = Integer.parseInt(splitstrmonth);

                int newintvalue = intmonth - 1;
                String splitstryear = splitstrdate[2];
                int intsplitstryear = Integer.parseInt(splitstryear);
                for (i = 0; i < newintvalue; i++) {
                    String strone = Integer.toString(newintvalue);
                    strone = months[i];
                    Log.v("strone", strone);
                }
                Log.v("month value", months[i].toString());
                String strentermonth = months[i].toString();

                if (i == intsplitstrday && strcurrentmonth == strentermonth
                        && year == intsplitstryear) {
                    list.add(String.valueOf(i) + "-BLUE" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy);
                    k++;
                } else {
                    list.add(String.valueOf(i) + "-WHITE" + "-"
                            + getMonthAsString(currentMonth) + "-" + yy);
                }
            } else {
                /*
                 * There are no more hilighted dates, so no need to split
                 * the date and do the checking, simply write the dates as
                 * they are...
                 */
                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);
        }

    }

    /**
     * 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<String, Integer> findNumberOfEventsPerMonth(int year,
            int month) {
        HashMap<String, Integer> map = new HashMap<String, Integer>();

        return map;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.screen_gridcell, parent, false);
        }

        // Get a reference to the Day gridcell
        gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
        gridcell.setOnClickListener(this);
        // ACCOUNT FOR SPACING

        // Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
        String[] day_color = list.get(position).split("-");
        Log.v("string value", day_color[1]);
        // Log.v("day",valuofday);

        String theday = day_color[0];
        String themonth = day_color[2];
        String theyear = day_color[3];

        if ((!eventsPerMonthMap.isEmpty()) && (eventsPerMonthMap != null)) {
            if (eventsPerMonthMap.containsKey(theday)) {
                num_events_per_day = (TextView) row
                        .findViewById(R.id.num_events_per_day);
                Integer numEvents = (Integer) eventsPerMonthMap.get(theday);
                num_events_per_day.setText(numEvents.toString());
            }
        }
        // Set the Day GridCell
        gridcell.setText(theday);

        gridcell.setTag(theday + "-" + themonth + "-" + theyear);

        // Log.d(tag, "Setting GridCell " + theday + "-" + themonth + "-"
        // + theyear);
        Log.v("str daycolor", day_color[1]);

        if (day_color[1].equals("GREY")) {
            gridcell.setTextColor(getResources()
                    .getColor(R.color.lightgray));
        }
        if (day_color[1].equals("WHITE")) {
            gridcell.setTextColor(getResources().getColor(
                    R.color.lightgray02));
        }
        if (day_color[1].equals("BLUE")) {
            gridcell.setBackgroundColor(getResources().getColor(
                    R.color.orrange));
        }
        return row;
    }

    @Override
    public void onClick(View view) {
        String date_month_year = (String) view.getTag();
        selectedDayMonthYearButton.setText("Selected: " + date_month_year);
        // Log.e("Selected date", date_month_year);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            // Log.d(tag, "Parsed Date: " + parsedDate.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    public int getCurrentDayOfMonth() {
        return currentDayOfMonth;
    }

    private void setCurrentDayOfMonth(int currentDayOfMonth) {
        this.currentDayOfMonth = currentDayOfMonth;
    }

    public void setCurrentWeekDay(int currentWeekDay) {
        this.currentWeekDay = currentWeekDay;
    }

    public int getCurrentWeekDay() {
        return currentWeekDay;
    }

}
公共类CalendarActivity扩展活动实现OnClickListener{
私有静态最终字符串tag=“CalendarActivity”;
私有文本视图当前月;
私人按钮选择Daymonthyearbutton;
每月私人影像浏览量;
下个月的私有ImageView;
私有GridView日历视图;
专用网格适配器;
私人日历;
@SuppressLint(“新API”)
私人int月,年;
@SuppressLint({“NewApi”、“NewApi”、“NewApi”、“NewApi”})
private final DateFormat dateFormatter=new DateFormat();
私有静态最终字符串dateTemplate=“MMMM yyyy”;
int i;
一日之弦;
内部结构;
数据库数据库;
字符串strmonthvalueofdatabase;
字符串[]标准日期;
字符串值;
ArrayList ArrayList=新的ArrayList();
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.my\u calendar\u视图);
db=新的db_数据库(此数据库);
_calendar=calendar.getInstance(Locale.getDefault());
月=_calendar.get(calendar.month)+1;
year=\u calendar.get(calendar.year);
//Log.d(标记,“日历实例:=”+“月:”+“月+”)+
//“年份:”
//+年);
选择Daymonthyearbutton=(按钮)此按钮
.findViewById(R.id.选择的日期/月份);
selectedDayMonthYearButton.setText(“Selected:”);
prevMonth=(ImageView)this.findViewById(R.id.prevMonth);
setOnClickListener(此);
currentMonth=(TextView)this.findViewById(R.id.currentMonth);
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime());
nextMonth=(ImageView)this.findViewById(R.id.nextMonth);
下一个月的setOnClickListener(此);
calendarView=(GridView)this.findViewById(R.id.calendar);
//草签
adapter=新的GridCellAdapter(getApplicationContext(),
R.id.日历\日\网格单元格,月,年);
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
/**
* 
*@param月
*@param年
*/
私有无效setGridCellAdapterToDate(整数月,整数年){
adapter=新的GridCellAdapter(getApplicationContext(),
R.id.日历\日\网格单元格,月,年);
_calendar.set(年,月-1,_calendar.get(calendar.DAY,OF_month));
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime());
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
@凌驾
公共void onClick(视图v){
如果(v==上个月){
如果(第11个月){
月=1;
年份++;
}否则{
月份++;
}
//Log.d(标记“在GridCellAdapter中设置下个月:”+“月份:”
//+月+年:“+年”;
setGridCellAdapterToDate(月,年);
}
}
@凌驾
公共空间{
//Log.d(标签“销毁视图…”);
super.ondestory();
}
//内部阶级
公共类GridCellAdapter扩展BaseAdapter实现OnClickListener{
私有静态最终字符串tag=“GridCellAdapter”;
私有最终上下文_上下文;
私人最终名单;
私人静态最终整数日_偏移=1;
私有最终字符串[]工作日=新字符串[]{“Sun”、“Mon”、“Tue”,
“星期三”、“星期四”、“星期五”、“星期六”};
私有最终字符串[]月={“一月”、“二月”、“三月”,
“四月”、“五月”、“六月”、“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”};
私人最终整数[]天每月={31,28,31,30,31,30,31,30,31,30,
31, 30, 31 };
私人国际日每月;
私人int currentDayOfMonth;
工作日内的私人时间;
私有按钮网格单元;
私有文本查看每天的事件数;
私有最终HashMap事件映射;
private final SimpleDataFormat dateFormatter=新SimpleDataFormat(
“dd-MMM-yyyy”);
//当月天数
公共GridCellAdapter(上下文,int textViewResourceId,
整数月,整数年){
超级();
这._context=context;
this.list=new ArrayList();
//Log.d(标记“==>月份的传入日期:“+Month+”)
//+“年:”+年);
日历=Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(calendar.DAY/u/u MONTH));
setCurrentWeekDay(calendar.get(calendar.DAY OF the u WEEK));
stryear=calendar.get(calendar.YEAR);
//Log.d(标记“newcalendar:=”+Calendar.getTime().toString());
//d(标记“CurrentDayOfWeek:+getCurrentWeekDay());
//d(标记“CurrentDayOfMonth:+getCurrentDayOfMonth());
打印月份(月、年);
eventsPerMonthMap=FindNumber of EventsPermonth(年、月);
}
私有字符串getMontHaString(int i){
返回月份[i];
}
私有字符串getWeekDayAsString(int i){
返回工作日[i];
}
private int getNumberOfDaysOfMonth(int i){
每月返回日[i];
}
公共字符串getItem(int位置){
返回列表。获取(位置);
}
@凌驾
public int getCount(){
返回list.size();
}
/**
*每月印刷
* 
*@param-mm
*@param-yy
*/
私人月(整数毫米,整数年){
//Log.d(tag,==>printmount:mm:+mm++++yy:+yy);
//在上留空的天数
//
int j = 0;
for (; j < newintvalue; j++) {
    String strone = Integer.toString(newintvalue);
    strone = months[j];
    Log.v("strone", strone);
}
Log.v("month value", months[j].toString());
String strentermonth = months[j].toString();