在以下代码中的何处添加onclicklistener函数,以选择日历中的网格视图

问题描述

我正在尝试为日历中的特定网格编写onClickListener函数,以便可以将事件添加到特定日期,因此请帮助我在哪里编写函数以选择作为日期的网格日历。

package com.example.myfitness;

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class Calendaradapter extends BaseAdapter {
    private List<Date> dateArray = new ArrayList();
    private Context mContext;
    private DateManager mDateManager;
    private LayoutInflater mLayoutInflater;

    //カスタムセルを拡張したらここでWigetを定義
    private static class ViewHolder {
        public TextView dateText;
    }

    public Calendaradapter(Context context){
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDateManager = new DateManager();
        dateArray = mDateManager.getDays();
    }

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

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.calendar_cell,null);
            holder = new ViewHolder();
            holder.dateText = convertView.findViewById(R.id.dateText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        //セルのサイズを指定
        float dp = mContext.getResources().getdisplayMetrics().density;
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp,(parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
        convertView.setLayoutParams(params);

        //日付のみ表示させる
        SimpleDateFormat dateFormat = new SimpleDateFormat("d",Locale.US);
        holder.dateText.setText(dateFormat.format(dateArray.get(position)));

        //当月以外のセルをグレーアウト
        if (mDateManager.isCurrentMonth(dateArray.get(position))){
            convertView.setBackgroundColor(Color.WHITE);
        }else {
            convertView.setBackgroundColor(Color.LTGRAY);
        }

        //日曜日を赤、土曜日を青に color used for weekends
        int colorId;
        switch (mDateManager.getDayOfWeek(dateArray.get(position))){
            case 1:
                colorId = Color.RED;
                break;
            case 7:
                colorId = Color.BLUE;
                break;

            default:
                colorId = Color.BLACK;
                break;
        }
        holder.dateText.setTextColor(colorId);

        return convertView;
    }

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

    @Override
    public Object getItem(int position) {
        return null;
    }

    //表示月を取得 display the current month
    public String getTitle(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM",Locale.US);
        return format.format(mDateManager.mCalendar.getTime());
    }

    //翌月表示 display the next month
    public void nextMonth(){
        mDateManager.nextMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }

    //前月表示 display the prevIoUs month
    public void prevMonth(){
        mDateManager.prevMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }
}

解决方法

请尝试此操作,请注意getView()方法中的最终代码:

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

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CalendarAdapter extends BaseAdapter {
    private List<Date> dateArray = new ArrayList();
    private Context mContext;
    private DateManager mDateManager;
    private LayoutInflater mLayoutInflater;

    //カスタムセルを拡張したらここでWigetを定義
    private static class ViewHolder {
        public TextView dateText;
    }

    public CalendarAdapter(Context context){
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDateManager = new DateManager();
        dateArray = mDateManager.getDays();
    }

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

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.calendar_cell,null);
            holder = new ViewHolder();
            holder.dateText = convertView.findViewById(R.id.dateText);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        //セルのサイズを指定
        float dp = mContext.getResources().getDisplayMetrics().density;
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp,(parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
        convertView.setLayoutParams(params);

        //日付のみ表示させる
        SimpleDateFormat dateFormat = new SimpleDateFormat("d",Locale.US);
        holder.dateText.setText(dateFormat.format(dateArray.get(position)));

        //当月以外のセルをグレーアウト
        if (mDateManager.isCurrentMonth(dateArray.get(position))){
            convertView.setBackgroundColor(Color.WHITE);
        }else {
            convertView.setBackgroundColor(Color.LTGRAY);
        }

        //日曜日を赤、土曜日を青に color used for weekends
        int colorId;
        switch (mDateManager.getDayOfWeek(dateArray.get(position))){
            case 1:
                colorId = Color.RED;
                break;
            case 7:
                colorId = Color.BLUE;
                break;

            default:
                colorId = Color.BLACK;
                break;
        }
        holder.dateText.setTextColor(colorId);
        holder.dateText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Your events for the particular date here!
            }
        });

        return convertView;
    }

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

    @Override
    public Object getItem(int position) {
        return dateArray.get(position);
    }

    //表示月を取得 display the current month
    public String getTitle(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM",Locale.US);
        return format.format(mDateManager.mCalendar.getTime());
    }

    //翌月表示 display the next month
    public void nextMonth(){
        mDateManager.nextMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }

    //前月表示 display the previous month
    public void prevMonth(){
        mDateManager.prevMonth();
        dateArray = mDateManager.getDays();
        this.notifyDataSetChanged();
    }
}
,

尝试一下:

    public class CalendarAdapter extends BaseAdapter {
        private List<Date> dateArray = new ArrayList();
        private Context mContext;
        private DateManager mDateManager;
        private LayoutInflater mLayoutInflater;
    
        //カスタムセルを拡張したらここでWigetを定義
        private static class ViewHolder {
            public TextView dateText;
        }
    
        public CalendarAdapter(Context context){
            mContext = context;
            mLayoutInflater = LayoutInflater.from(mContext);
            mDateManager = new DateManager();
            dateArray = mDateManager.getDays();
        }
    
        @Override
        public int getCount() {
            return dateArray.size();
        }
    
        @Override
        public View getView(int position,ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.calendar_cell,null);
                holder = new ViewHolder();
                holder.dateText = convertView.findViewById(R.id.dateText);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
    
            //セルのサイズを指定
            float dp = mContext.getResources().getDisplayMetrics().density;
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(parent.getWidth()/7 - (int)dp,(parent.getHeight() - (int)dp * mDateManager.getWeeks() ) / mDateManager.getWeeks());
            convertView.setLayoutParams(params);
    
            //日付のみ表示させる
            SimpleDateFormat dateFormat = new SimpleDateFormat("d",Locale.US);
            holder.dateText.setText(dateFormat.format(dateArray.get(position)));
    
            //当月以外のセルをグレーアウト
            if (mDateManager.isCurrentMonth(dateArray.get(position))){
                convertView.setBackgroundColor(Color.WHITE);
            }else {
                convertView.setBackgroundColor(Color.LTGRAY);
            }
    
            //日曜日を赤、土曜日を青に color used for weekends
            int colorId;
            switch (mDateManager.getDayOfWeek(dateArray.get(position))){
                case 1:
                    colorId = Color.RED;
                    break;
                case 7:
                    colorId = Color.BLUE;
                    break;
    
                default:
                    colorId = Color.BLACK;
                    break;
            }
            holder.dateText.setTextColor(colorId);

convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Your code here
            }
        });
    
            return convertView;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        //表示月を取得 display the current month
        public String getTitle(){
            SimpleDateFormat format = new SimpleDateFormat("yyyy.MM",Locale.US);
            return format.format(mDateManager.mCalendar.getTime());
        }
    
        //翌月表示 display the next month
        public void nextMonth(){
            mDateManager.nextMonth();
            dateArray = mDateManager.getDays();
            this.notifyDataSetChanged();
        }
    
        //前月表示 display the previous month
        public void prevMonth(){
            mDateManager.prevMonth();
            dateArray = mDateManager.getDays();
            this.notifyDataSetChanged();
        }
    }