在GridView的BaseAdapter的getView()中出现问题

时间:2013-05-04 11:41:30

标签: android sqlite

enter image description here

我正在创建一个日历应用。在哪个用户应该输入事件。事件将存储在本地数据库中。我创建了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 请给我重播适当的解决方案。 提前谢谢

0 个答案:

没有答案