728x90

안드로이드 달력 화면에 뿌려줄 GridCellAdapter 코드는 아래와 같다.


package com.link2me.android.adpater;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.link2me.android.calendar.R;
import com.link2me.android.item.Calendar_Item;
import com.link2me.android.util.CalendarHelper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

public class GridCellAdapter extends BaseAdapter implements View.OnClickListener {
    private static final String tag = GridCellAdapter.class.getSimpleName();
    private final Context mContext;

    private final ArrayList<Calendar_Item> calList;
    private LinearLayout gridcell_layout;
    private TextView gridcell_Day;
    private TextView gridcell_LunarDay;
    private TextView gridcell_Event;
    private final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy");

    public GridCellAdapter(Context context, ArrayList<Calendar_Item> list) {
        mContext = context;
        calList = list;
    }

    public Calendar_Item getItem(int position) {
        return calList.get(position);
    }

    @Override
    public int getCount() {
        return calList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (view == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.calendar_cell, parent, false);
        }

        // Get a reference to the Day gridcell_layout
        gridcell_layout = (LinearLayout) view.findViewById(R.id.calendar_day_gridcell);
        gridcell_Day = view.findViewById(R.id.dayTV);
        gridcell_LunarDay = view.findViewById(R.id.lunardayTV);
        gridcell_Event = view.findViewById(R.id.eventTV);
        gridcell_layout.setOnClickListener(this);

        String theday = calList.get(position).getDay();
        int themonth = Integer.parseInt(calList.get(position).getMonth());
        String theyear = calList.get(position).getYear();
        String holiday = calList.get(position).getEvent();

        // Set the Day GridCell
        gridcell_Day.setText(theday);
        gridcell_LunarDay.setText(CalendarHelper.Sol2Lun(theyear, String.valueOf(themonth-1),theday)); //0 ~ 11월로 인식하므로 - 1
        if(holiday.length()>0){
            gridcell_Event.setText(holiday);
        } else {
            gridcell_Event.setText("");
        }
        gridcell_layout.setTag(theday + "-" + themonth + "-" + theyear);

        if(calList.get(position).getColor().equals("GRAY")){
            gridcell_Day.setTextColor(Color.LTGRAY);
        }
        if(calList.get(position).getColor().equals("BLACK")){
            gridcell_Day.setTextColor(Color.BLACK);
        }
        if(calList.get(position).getColor().equals("RED")){
            gridcell_Day.setTextColor(Color.RED);
            gridcell_Event.setTextColor(Color.RED);
        }
        if(calList.get(position).getColor().equals("BLUE")){
            gridcell_Day.setTextColor(Color.BLUE);
        }
        if(calList.get(position).getColor().equals("CYAN")){
            gridcell_layout.setBackgroundColor(Color.CYAN);
        }
        return view;
    }

    @Override
    public void onClick(View view) {
        String date_month_year = (String) view.getTag();
        Log.e("Selected date", date_month_year);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}


calendar_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/calendar_day_gridcell"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/day_cell_bg"
    android:orientation="vertical"
    android:padding="7dp">

    <TextView
        android:id="@+id/dayTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8"
        android:textStyle="bold"
        android:textSize="24sp" />

    <TextView
        android:id="@+id/lunardayTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="00.16"
        android:textSize="10sp" />

    <TextView
        android:id="@+id/eventTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:maxLines="1"
        android:text="기념일"
        android:textColor="#ff0000" />

</LinearLayout>



블로그 이미지

Link2Me

,