2

在此处输入图像描述

我正在创建一个日历应用程序。用户应在其中输入事件。事件将存储在本地数据库中。我创建了 BaseAdapter 类。在这个 BaseAdapters getView() 方法中发生了一些我无法理解的事情。请参阅我附在此处的屏幕截图。在此屏幕中,您将找到一个日历。在您调用的右下角看到一个白灰色加号按钮。当用户在日历的网格视图中按下此按钮时,所有网格外壳将显示绿色的小加号。这一切都在 BaseAdapters 的 GetView() 方法中完成。

请在此处查看代码:

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

现在在getView()方法里面你可以看到当用户按下网格外壳内的小绿色加号按钮时,这个日期将存储在本地数据库中。而在

holder.btn_img_plus_internal.setOnClickListener(new OnClickListener() { 

我有调用方法notifyDataSetChanged();,这就是为什么getView()会再次调用并从本地数据库中获取数据并在此处制作不同图像的原因。当我单击任何绿色小加号按钮图像将被替换为附加图像时,它就完成了。但是当我第二次按下任何图像时,其他网格中的一些其他图像也会被替换。

例如,当我第一次按下 15 Dated Greed green-plus 图像时,一旦我按下它就会被替换。这正是我想要的。但是,当我按下例如 12 张过时的图像时,它也会替换其他图像。实际上我想要做的是当我按下颗粒绿色加图像时,只有那个图像想要被替换。我按下的最后一张图片应该在之前也保持在那里替换。在制作我的 android 应用程序的视频后,我还在这里添加了链接。请看这些视频: http: //fortunemobiletech.comyr.com/ccc/calender_android_app.swf 请用适当的解决方案给我重播。先感谢您

4

0 回答 0