Android日历Gridview - 如何保存我的日历中每个单元格的信息?

时间:2011-08-28 11:41:13

标签: android calendar position cell

我正在尝试构建一个看起来像Android内部日历的日历。我想知道的是如何在我的视图中跟踪每个单元格。

我有一个扩展基本适配器的Gridcell适配器,这里是代码:

    public View getView(int position, View convertView, ViewGroup parent)
        {

            int[][] MatrixPointer = new int[5][7];



            View row = convertView;
            if (row == null)
                {
                    LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    row = inflater.inflate(R.layout.calendar_day_gridcell, parent, false);
                }

            // Get a reference to the Day gridcell
            gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);


            if(position==34||position==6||position==13||position==20||position==27)
            {
                View v=(View) row.findViewById(R.id.right_border);
                v.setVisibility(View.GONE);
            }

            if(position>=7)
            {
                View v;
                v=(View) row.findViewById(R.id.top_border_double);
                v.setVisibility(View.GONE);
            }


            if(position<=27)
            {
                View v=(View) row.findViewById(R.id.bottom_border_double);
                v.setVisibility(View.GONE);v=(View) row.findViewById(R.id.bottom_border);
                v.setVisibility(View.GONE);
            }
            // ACCOUNT FOR SPACING

            Log.d(tag, "Current Day: " + getCurrentDayOfMonth());
            String[] day_color = list.get(position).split("-");
            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 + " \n Position: "+position);

            if (day_color[1].equals("GREY"))
                {
                    gridcell.setTextColor(Color.LTGRAY);
                }
            if (day_color[1].equals("WHITE"))
                {
                    gridcell.setTextColor(Color.DKGRAY);
                }
            if (day_color[1].equals("BLUE"))
                {
                    gridcell.setTextColor(getResources().getColor(R.color.static_text_color));

                }
            return row;

        }


    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;
        }
    @Override
    public void onClick(View v) {

    }
}

当我使用onClick方法时,它没有做任何事情,比如说:

            gridcell.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    gridcell.setText("TESTING");

                }
            });
点击时,

不会向我的View更新任何内容。尝试在我的课堂外设置一个点击监听器,在我的Activity中,但同样没有结果。我想知道每个gridcells位置和其他信息(如文本,颜色)。当我在一个函数中更改gridcell的颜色时,它会破坏我的设计。例如,

            if (day_color[1].equals("BLUE"))
                {                       gridcell.setBackgroundColor(Color.BLUE);

                }

如果我这样做会弄乱我的设计。

希望您明白我的意思并查看我的代码。

谢谢! 拉杜

2 个答案:

答案 0 :(得分:1)

<GridView
    android:id="@+id/gv_series"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="1dip"
    android:layout_marginRight="1dip"
    android:horizontalSpacing="1dip"
    android:numColumns="auto_fit"
    android:verticalSpacing="1dip" android:listSelector="@drawable/item_selector">

</GridView>

然后,设置GridView的背景使用一种颜色。

答案 1 :(得分:1)

请试试这个:

if (day_color[1].equals("BLUE")) {              
    gridcell.setTextColor(getResources().getColor(R.color.static_text_color));
    gridcell.setBackgroundDrawable(null);
    gridcell.setBackgroundResource(R.drawable.calendar_button_selector_day);
}

您必须删除分配给网格单元格中按钮的背景。并分配新的背景可绘制资源。我试过这个。它对我有用。