2

enter image description here

I am creating a calender app. In which user should enter events. Events will be stored in local database. I have created BaseAdapter class. In this BaseAdapters getView() method something happened i cant understand. Please see Screen shot i have attached here. In this screen you will find a calender. At the right bottom side you call see a White-Gray Colored plus button. When user press this button in calender's grid view all grid shells will display green colored small plus sign. It is all done in BaseAdapters GetView() Method.

Please see code here :

package com.examples;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import android.R.integer;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class GridCellAdapter extends BaseAdapter{
    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 final int month, year;
    private int daysInMonth, prevMonthDays;
    private int currentDayOfMonth;
    private int currentWeekDay;
    private TextView num_events_per_day;
    private final HashMap eventsPerMonthMap;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");
    boolean token;
//  boolean boo;
    LayoutInflater inflater;
    ArrayList<String> arrLst;
    private SharedPreferences prefs;
    private String prefName = "MyPref";
//  List<String> list_selected = new ArrayList<String>();
    String clicked_day;
    boolean personla_token;
    DBAdapter db;

    public GridCellAdapter(Context context, int textViewResourceId, int month, int year, boolean token)
    {
        super();
        this._context = context;
        this.list = new ArrayList<String>();
        this.month = month;
        this.year = year;
        this.token = token;
        db = new DBAdapter(context);
        arrLst = new ArrayList<String>();
//      list_selected = new ArrayList<String>();
        Log.v("","Adapter Token : "+token);
        Calendar calendar = Calendar.getInstance();
        prefs = context.getSharedPreferences(prefName, context.MODE_PRIVATE);
//      Log.v("","Check this preference : "+prefs.getString("textvalue", ""));
        setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
        setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
        // Print Month
        printMonth(month, year);
        DBAdapter db;

        // Find Number of Events
        eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
        inflater = LayoutInflater.from(context);

    }

    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()
    {
//      Log.v("","List Size : "+list.size());
        return list.size();
    }

    private void printMonth(int mm, int yy)
    {
        // The number of days to leave blank at
        // the start of this month.
        int trailingSpaces = 0;
        int leadSpaces = 0;
        int daysInPrevMonth = 0;
        int prevMonth = 0;
        int prevYear = 0;
        int nextMonth = 0;
        int nextYear = 0;

        int currentMonth = mm - 1;
//      Log.v("","Current month from printMonth : "+currentMonth);
        String currentMonthName = getMonthAsString(currentMonth);
//      Log.v("","This is month in string : "+currentMonthName);
        daysInMonth = getNumberOfDaysOfMonth(currentMonth);


        // Gregorian Calendar : MINUS 1, set to FIRST OF MONTH
        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;
//          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.v(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;
        trailingSpaces = currentWeekDay;

//      Log.v("","Current week days : "+currentWeekDay);
//      Log.v("","Trailing spaces : "+trailingSpaces);
        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);
        }

        // 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);
        }
    }

    /**
     * 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;
        }
    @Override
    public long getItemId(int position)
    {
        return position;
    }
    String full_date;
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;   
        if (convertView == null){
            holder= new ViewHolder();
            convertView = inflater.inflate(R.layout.calendar_day_gridcell, null);

            holder.gridcell = (Button) convertView.findViewById(R.id.calendar_day_gridcell);
            holder.btn_img_plus_internal = (ImageButton)convertView.findViewById(R.id.btn_plus_green);

            convertView.setTag(holder);
        }
        else{
            holder = (ViewHolder) convertView.getTag();
        }

        // Get a reference to the Day gridcell

        // ACCOUNT FOR SPACING

        String[] day_color = list.get(position).split("-");
        String theday   = day_color[0];
        String themonth = day_color[2];
        String theyear  = day_color[3];

        full_date = theday+"-"+themonth+"-"+theyear;

        //check everytime when gridcell created if date inside database replce image with specific entry 
        db.open();
        Cursor c = db.getAllData();
        if(c.moveToFirst()){
            do {
                if(full_date.equals(c.getString(1))){
                    holder.btn_img_plus_internal.setVisibility(View.VISIBLE);
                        holder.btn_img_plus_internal.setBackgroundResource(R.drawable.event_3);
                }
            } while (c.moveToNext());
        }

        c.close();
        db.close();

        if(token){
            holder.btn_img_plus_internal.setVisibility(View.VISIBLE);
        }

        holder.btn_img_plus_internal.setTag(full_date);
        holder.btn_img_plus_internal.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                clicked_day = (String) v.getTag();

                    db.open();
                        db.insert(clicked_day, prefs.getString("textvalue", ""));
                    db.close();
                    notifyDataSetChanged();
            }
        });

        // Set the Day GridCell
        holder.gridcell.setText(theday);
        return convertView;
    }

    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;
    }

    public class ViewHolder{
        Button gridcell;
        ImageButton btn_img_plus_internal;
    }
}

Now inside getView() Method you can see When user press small-green-plus button inside grid shell this date will be stored in Local database. And in

holder.btn_img_plus_internal.setOnClickListener(new OnClickListener() { 

i have call method notifyDataSetChanged(); that's why again getView() will be called and data from local database will be fetched and make here a different image. And it is done when i click any green-small-plus button image will be replaced like attached image. But when i press any image second time there are some other images from other grid replaced too.

For example when first time i pressed 15 Dated Greed green-plus image then it will replaced once i press it. It is exactly what i want. But when i press after for example 12 dated image it will replaced other image too. Actually what i want to do is when i press particulate green-plus image only that image want to be replaced. and last image that i pressed should be before also keep replaced there. I have also add link here after make video of my android app. Please see these video : http://fortunemobiletech.comyr.com/ccc/calender_android_app.swf Please give me replay with proper solutions. Thank you in advance

  • Possible duplicate of [Add events to this Calender](http://stackoverflow.com/questions/8587496/add-events-to-this-calender) – pRaNaY Oct 26 '15 at 10:24

0 Answers0