mardi 24 novembre 2015

how to get event category on same date from custom calendar

this is my calendar activity.inserted data from this activity... second is my calendar adapter class when i get data from sqlite i want to find on same date how many events are present and having which categories...

 MySQLiteHelper db = new MySQLiteHelper(this);
            SharedPreferences prefs = PreferenceManager.
                    getDefaultSharedPreferences(this);

            if (!prefs.contains("insertedInDB")) {
                // insert in DB
                // add events in database
                db.addEvent(new CalendarEvent("Birthday", "2015-11-15", "Location-House", "aa"));
          //event name,event date,event location,event category
                db.addEvent(new CalendarEvent("Birthday", "2015-11-15", "Location- House", "bb"));
                db.addEvent(new CalendarEvent("Mobile Computing Paper", "2015-11-01", "Location- college", "bb"));
                db.addEvent(new CalendarEvent("Birthday", "2015-11-01", "Location-House", "bb"));          
                db.addEvent(new CalendarEvent("31th December", "2015-11-30", "Party Location-House" , "cc"));


                // create key in prefs
                SharedPreferences.Editor editor = prefs.edit();
                editor.putBoolean("insertedInDB", true);
                editor.commit();
            } else {
                // no need to insert in db
            }

CalendarAdapter.java:

import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.GregorianCalendar;
    import java.util.List;
    import java.util.Locale;

    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.graphics.Color;
    import android.graphics.drawable.GradientDrawable;
    import android.util.TypedValue;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.LinearLayout;
    import android.widget.TextView;



    public class CalendarAdapter extends BaseAdapter {
        private Context context;

        private java.util.Calendar month;
        public GregorianCalendar pmonth;

        /*-----------calendar instance for previous month for getting complete view-------------------*/

        public GregorianCalendar pmonthmaxset;
        private GregorianCalendar selectedDate;
        int firstDay;
        int maxWeeknumber;
        int maxP;
        int calMaxP;
        int lastWeekDay;
        int leftDays;
        int mnthlength;
        BadgeView badge;
        String itemvalue, curentDateString;
        DateFormat df;
        LinearLayout event_dateview_layout;
        View first_event_date_view, second_event_date_view, third_event_date_view, fourth_event_date_view;
        private ArrayList<String> items;
        public static List<String> day_string;
        private View previousView;
        public ArrayList<CalendarEvent> event_calendar_arr;

        public CalendarAdapter(Context context, GregorianCalendar monthCalendar, ArrayList<CalendarEvent> event_calendar_arr) {

            this.event_calendar_arr = event_calendar_arr;
            CalendarAdapter.day_string = new ArrayList<String>();
            Locale.setDefault(Locale.US);
            month = monthCalendar;
            selectedDate = (GregorianCalendar) monthCalendar.clone();
            this.context = context;
            month.set(GregorianCalendar.DAY_OF_MONTH, 1);
            this.items = new ArrayList<String>();
            df = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            curentDateString = df.format(selectedDate.getTime());
            refreshDays();

        }

        public void setItems(ArrayList<String> items) {
            for (int i = 0; i != items.size(); i++) {
                if (items.get(i).length() == 1) {
                    items.set(i, "0" + items.get(i));
                }
            }
            this.items = items;
        }

        public int getCount() {

            return day_string.size();
        }

        public Object getItem(int position) {

            return day_string.get(position);
        }

        public long getItemId(int position) {

            return 0;
        }

        // create a new view for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            TextView dayView;

            if (convertView == null) { // if it's not recycled, initialize some
                // attributes
                LayoutInflater vi = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.event_date_view, parent, false);
                event_dateview_layout = (LinearLayout) v.findViewById(R.id.event_dateview_layout);
                first_event_date_view = v.findViewById(R.id.first_event_date_view);
                second_event_date_view = v.findViewById(R.id.second_event_date_view);
                third_event_date_view = v.findViewById(R.id.third_event_date_view);
                fourth_event_date_view = v.findViewById(R.id.fourth_event_date_view);

            }
            dayView = (TextView) v.findViewById(R.id.date_text);
            String[] separatedTime = day_string.get(position).split("-");

            String gridvalue = separatedTime[2].replaceFirst("^0*", "");
            if ((Integer.parseInt(gridvalue) > 1) && (position < firstDay)) {
                dayView.setTextColor(Color.RED);
                dayView.setClickable(false);
                dayView.setFocusable(false);
            } else if ((Integer.parseInt(gridvalue) < 7) && (position > 28)) {
                dayView.setTextColor(Color.RED);
                dayView.setClickable(false);
                dayView.setFocusable(false);
            } else {
                // setting curent month's days in blue color.
                dayView.setTextColor(Color.WHITE);
            }


            if (day_string.get(position).equals(curentDateString)) {

                event_dateview_layout.setBackgroundColor(Color.RED);

            } else {
                v.setBackgroundColor(Color.parseColor("#343434"));
            }


            dayView.setText(gridvalue);

            // create date string for comparison
            String date = day_string.get(position);

            if (date.length() == 1) {
                date = "0" + date;
            }
            String monthStr = "" + (month.get(GregorianCalendar.MONTH) + 1);
            if (monthStr.length() == 1) {
                monthStr = "0" + monthStr;
            }

            // show icon if date is not empty and it exists in the items array
            /*ImageView iw = (ImageView) v.findViewById(R.id.date_icon);
            if (date.length() > 0 && items != null && items.contains(date)) {
                iw.setVisibility(View.VISIBLE);
            } else {
                iw.setVisibility(View.GONE);
            }
            */

            setEventView(v, position, dayView);

            return v;
        }

        public View setSelected(View view, int pos) {
            if (previousView != null) {
                previousView.setBackgroundColor(Color.parseColor("#343434"));
            }

            view.setBackgroundColor(Color.CYAN);


            int len = day_string.size();
            if (len > pos) {
                if (day_string.get(pos).equals(curentDateString)) {

                } else {

                    previousView = view;

                }

            }


            return view;
        }

        public void refreshDays() {
            // clear items
            items.clear();
            day_string.clear();
            Locale.setDefault(Locale.US);
            pmonth = (GregorianCalendar) month.clone();
            // month start day. ie; sun, mon, etc
            firstDay = month.get(GregorianCalendar.DAY_OF_WEEK);
            // finding number of weeks in current month.
            maxWeeknumber = month.getActualMaximum(GregorianCalendar.WEEK_OF_MONTH);
            // allocating maximum row number for the gridview.
            mnthlength = maxWeeknumber * 7;
            maxP = getMaxP(); // previous month maximum day 31,30....
            calMaxP = maxP - (firstDay - 1);// calendar offday starting 24,25 ...
            /**
             * Calendar instance for getting a complete gridview including the three
             * month's (previous,current,next) dates.
             */
            pmonthmaxset = (GregorianCalendar) pmonth.clone();
            /**
             * setting the start date as previous month's required date.
             */
            pmonthmaxset.set(GregorianCalendar.DAY_OF_MONTH, calMaxP + 1);

            /**
             * filling calendar gridview.
             */
            for (int n = 0; n < mnthlength; n++) {

                itemvalue = df.format(pmonthmaxset.getTime());
                pmonthmaxset.add(GregorianCalendar.DATE, 1);
                day_string.add(itemvalue);

            }
        }

        private int getMaxP() {
            int maxP;
            if (month.get(GregorianCalendar.MONTH) == month
                    .getActualMinimum(GregorianCalendar.MONTH)) {
                pmonth.set((month.get(GregorianCalendar.YEAR) - 1),
                        month.getActualMaximum(GregorianCalendar.MONTH), 1);
            } else {
                pmonth.set(GregorianCalendar.MONTH,
                        month.get(GregorianCalendar.MONTH) - 1);
            }
            maxP = pmonth.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);

            return maxP;
        }

        public void setEventView(View v, int pos, TextView txt) {

            //get array size in len
            int len = CalendarEvent.event_calendar_arr.size();
            for (int i = 0; i < len; i++) {
                CalendarEvent cal_obj = CalendarEvent.event_calendar_arr.get(i);
                //get event date
                String category = cal_obj.getEventCate();
                String date = cal_obj.getEventDate();

                int len1 = day_string.size();
                if (len1 > pos) {

                    if (day_string.get(pos).equals(date)) {

                        if ("aa".equals(category)) {
                            event_dateview_layout.setBackgroundColor(Color.CYAN);
                        } else if ("bb".equals(category)) {
                            event_dateview_layout.setBackgroundColor(Color.BLUE);
                        } else if ("cc".equals(category)) {
                            event_dateview_layout.setBackgroundColor(Color.GREEN);
                        } else if ("dd".equals(category)) {
                            event_dateview_layout.setBackgroundColor(Color.YELLOW);
                        } else if ("aa,bb".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.CYAN);
                            second_event_date_view.setBackgroundColor(Color.CYAN);
                            third_event_date_view.setBackgroundColor(Color.BLUE);
                            fourth_event_date_view.setBackgroundColor(Color.BLUE);
                        } else if ("bb,cc".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.BLUE);
                            second_event_date_view.setBackgroundColor(Color.BLUE);
                            third_event_date_view.setBackgroundColor(Color.GREEN);
                            fourth_event_date_view.setBackgroundColor(Color.GREEN);
                        } else if ("cc,dd".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.GREEN);
                            second_event_date_view.setBackgroundColor(Color.GREEN);
                            third_event_date_view.setBackgroundColor(Color.YELLOW);
                            fourth_event_date_view.setBackgroundColor(Color.YELLOW);
                        } else if ("aa,bb,cc".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.CYAN);
                            second_event_date_view.setBackgroundColor(Color.BLUE);
                            third_event_date_view.setBackgroundColor(Color.GREEN);
                            fourth_event_date_view.setBackgroundColor(Color.GREEN);
                        } else if ("bb,cc,dd".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.CYAN);
                            second_event_date_view.setBackgroundColor(Color.BLUE);
                            third_event_date_view.setBackgroundColor(Color.GREEN);
                            fourth_event_date_view.setBackgroundColor(Color.GREEN);
                        } else if ("aa,bb,cc,dd".equals(category)) {
                            first_event_date_view.setBackgroundColor(Color.CYAN);
                            second_event_date_view.setBackgroundColor(Color.BLUE);
                            third_event_date_view.setBackgroundColor(Color.GREEN);
                            fourth_event_date_view.setBackgroundColor(Color.YELLOW);
                        }

                        txt.setTextColor(Color.WHITE);

                    }
                }
            }


        }

        public void getPositionList(String date, final Activity act) {

            String event_date = null;
            String event_name = null;
            String event_desc = null;
            int len = 0;
            len = CalendarEvent.event_calendar_arr.size();
            for (int i = 0; i < len; i++) {
                CalendarEvent cal_event = CalendarEvent.event_calendar_arr.get(i);

                event_date = cal_event.getEventDate();
                event_name = cal_event.getEventName();
                event_desc = cal_event.getEventDesc();


                if (date.equals(event_date)) {

                    Intent in = new Intent(context, Event.class);
                    in.putExtra("EVENT_DATE", event_date);
                    in.putExtra("EVENT_NAME", event_name);
                    in.putExtra("EVENT_DESC", event_desc);
                    context.startActivity(in);


    //                Toast.makeText(context, "You have event on this date: " + event_date, Toast.LENGTH_LONG).show();
    //                new AlertDialog.Builder(context)
    //                        .setIcon(android.R.drawable.ic_dialog_alert)
    //                        .setTitle("Date: " + event_date)
    //                        .setMessage("Event: " + event_message)
    //                        .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {
    //                            public void onClick(DialogInterface dialog, int which) {
    //                                act.finish();
    //                            }
    //                        }).show();
    //                break;
                }
            }
        }


    }

Aucun commentaire:

Enregistrer un commentaire