Android 网格单元格中的自定义日历膨胀错误
主课Android 网格单元格中的自定义日历膨胀错误,android,adapter,layout-inflater,Android,Adapter,Layout Inflater,主课 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.member_profile); _calendar = Calendar.getInstance(Locale.getDe
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.member_profile);
_calendar = Calendar.getInstance(Locale.getDefault());
month = _calendar.get(Calendar.MONTH) + 1;
year = _calendar.get(Calendar.YEAR);
Log.d(tag, "Calendar Instance:= " + "Month: " + month + " " + "Year: "
+ year);
selectedDayMonthYearButton = (Button) this
.findViewById(R.id.selectedDayMonthYear);
selectedDayMonthYearButton.setText("Selected: ");
prevMonth = (ImageView) this.findViewById(R.id.prevMonth);
prevMonth.setOnClickListener(this);
currentMonth = (TextView) this.findViewById(R.id.currentMonth);
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime()));
nextMonth = (ImageView) this.findViewById(R.id.nextMonth);
nextMonth.setOnClickListener(this);
calendarView = (GridView) this.findViewById(R.id.calendar);
// Initialised
adapter = new GridCellAdapter(getApplicationContext(),
R.id.calendar_day_gridcell, month, year);
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
private void setGridCellAdapterToDate(int month, int year) {
adapter = new GridCellAdapter(getApplicationContext(),
R.id.calendar_day_gridcell, month, year);
_calendar.set(year, month - 1, _calendar.get(Calendar.DAY_OF_MONTH));
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime()));
adapter.notifyDataSetChanged();
calendarView.setAdapter(adapter);
}
@Override
public void onClick(View v) {
if (v == prevMonth) {
if (month <= 1) {
month = 12;
year--;
} else {
month--;
}
Log.d(tag, "Setting Prev Month in GridCellAdapter: " + "Month: "
+ month + " Year: " + year);
setGridCellAdapterToDate(month, year);
}
if (v == nextMonth) {
if (month > 11) {
month = 1;
year++;
} else {
month++;
}
Log.d(tag, "Setting Next Month in GridCellAdapter: " + "Month: "
+ month + " Year: " + year);
setGridCellAdapterToDate(month, year);
}
}
@Override
public void onDestroy() {
Log.d(tag, "Destroying View ...");
super.onDestroy();
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.member_profile);
_calendar=calendar.getInstance(Locale.getDefault());
月=_calendar.get(calendar.month)+1;
year=\u calendar.get(calendar.year);
Log.d(标记,“日历实例:=”+“月:”+“月+”+“年:”
+年份);
选择Daymonthyearbutton=(按钮)此按钮
.findViewById(R.id.选择的日期/月份);
selectedDayMonthYearButton.setText(“Selected:”);
prevMonth=(ImageView)this.findViewById(R.id.prevMonth);
setOnClickListener(此);
currentMonth=(TextView)this.findViewById(R.id.currentMonth);
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime());
nextMonth=(ImageView)this.findViewById(R.id.nextMonth);
下一个月的setOnClickListener(此);
calendarView=(GridView)this.findViewById(R.id.calendar);
//草签
adapter=新的GridCellAdapter(getApplicationContext(),
R.id.日历\日\网格单元格,月,年);
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
私有无效setGridCellAdapterToDate(整数月,整数年){
adapter=新的GridCellAdapter(getApplicationContext(),
R.id.日历\日\网格单元格,月,年);
_calendar.set(年,月-1,_calendar.get(calendar.DAY,OF_month));
currentMonth.setText(DateFormat.format(dateTemplate,
_calendar.getTime());
adapter.notifyDataSetChanged();
calendarView.setAdapter(适配器);
}
@凌驾
公共void onClick(视图v){
如果(v==上个月){
如果(第11个月){
月=1;
年份++;
}否则{
月份++;
}
Log.d(标记“在GridCellAdapter中设置下个月:”+“月份:”
+月+年:“+年”;
setGridCellAdapterToDate(月,年);
}
}
@凌驾
公共空间{
Log.d(标签“销毁视图…”);
super.ondestory();
}
这是主活动类,然后代码跳转到adapter类,在getView()中为适配器扩展xml代码时遇到问题
GridCellAdapter.class
public class GridCellAdapter extends BaseAdapter implements View.OnClickListener {
private static final String tag = "GridCellAdapter";
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 int daysInMonth;
private int currentDayOfMonth;
private int currentWeekDay;
private Button gridcell;
private TextView num_events_per_day;
private Button selectedDayMonthYearButton;
private final HashMap<String, Integer> eventsPerMonthMap;
private final SimpleDateFormat dateFormatter = new SimpleDateFormat(
"dd-MMM-yyyy");
// Days in Current Month
public GridCellAdapter(Context context, int textViewResourceId,
int month, int year) {
super();
this._context = context;
this.list = new ArrayList<String>();
Log.d(tag, "==> Passed in Date FOR Month: " + month + " "
+ "Year: " + year);
Calendar calendar = Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(Calendar.DAY_OF_MONTH));
setCurrentWeekDay(calendar.get(Calendar.DAY_OF_WEEK));
Log.d(tag, "New Calendar:= " + calendar.getTime().toString());
Log.d(tag, "CurrentDayOfWeek :" + getCurrentWeekDay());
Log.d(tag, "CurrentDayOfMonth :" + getCurrentDayOfMonth());
// Print Month
printMonth(month, year);
// Find Number of Events
eventsPerMonthMap = findNumberOfEventsPerMonth(year, month);
}
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() {
return list.size();
}
/**
* Prints Month
*
* @param mm
* @param yy
*/
private void printMonth(int mm, int yy) {
Log.d(tag, "==> printMonth: mm: " + mm + " " + "yy: " + yy);
int trailingSpaces = 0;
int daysInPrevMonth = 0;
int prevMonth = 0;
int prevYear = 0;
int nextMonth = 0;
int nextYear = 0;
int currentMonth = mm - 1;
String currentMonthName = getMonthAsString(currentMonth);
daysInMonth = getNumberOfDaysOfMonth(currentMonth);
Log.d(tag, "Current Month: " + " " + currentMonthName + " having "
+ daysInMonth + " days.");
GregorianCalendar cal = new GregorianCalendar(yy, currentMonth, 1);
Log.d(tag, "Gregorian Calendar:= " + cal.getTime().toString());
if (currentMonth == 11) {
prevMonth = currentMonth - 1;
daysInPrevMonth = getNumberOfDaysOfMonth(prevMonth);
nextMonth = 0;
prevYear = yy;
nextYear = yy + 1;
Log.d(tag, "*->PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
} 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.d(tag, "***---> PrevYear: " + prevYear + " PrevMonth:"
+ prevMonth + " NextMonth: " + nextMonth
+ " NextYear: " + nextYear);
}
int currentWeekDay = cal.get(Calendar.DAY_OF_WEEK) - 1;
trailingSpaces = currentWeekDay;
Log.d(tag, "Week Day:" + currentWeekDay + " is "
+ getWeekDayAsString(currentWeekDay));
Log.d(tag, "No. Trailing space to Add: " + trailingSpaces);
Log.d(tag, "No. of Days in Previous Month: " + daysInPrevMonth);
if (cal.isLeapYear(cal.get(Calendar.YEAR)))
if (mm == 2)
++daysInMonth;
else if (mm == 3)
++daysInPrevMonth;
// 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<String, Integer> findNumberOfEventsPerMonth(int year,
int month) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
return map;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) _context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.gridecell, parent, false);
}
// Get a reference to the Day gridcell
gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
gridcell.setOnClickListener(this);
// 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);
return row;
}
@Override
public void onClick(View view) {
String date_month_year = (String) view.getTag();
selectedDayMonthYearButton.setText("Selected: " + date_month_year);
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();
}
}
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;
}
}
公共类GridCellAdapter扩展BaseAdapter实现View.OnClickListener{
私有静态最终字符串tag=“GridCellAdapter”;
私有最终上下文_上下文;
私人最终名单;
私人静态最终整数日_偏移=1;
私有最终字符串[]工作日=新字符串[]{“Sun”、“Mon”、“Tue”,
“星期三”、“星期四”、“星期五”、“星期六”};
私有最终字符串[]月={“一月”、“二月”、“三月”,
“四月”、“五月”、“六月”、“七月”、“八月”、“九月”,
“十月”、“十一月”、“十二月”};
私人最终整数[]天每月={31,28,31,30,31,30,31,30,31,30,
31, 30, 31 };
私人国际日每月;
私人int currentDayOfMonth;
工作日内的私人时间;
私有按钮网格单元;
私有文本查看每天的事件数;
私人按钮选择Daymonthyearbutton;
私有最终HashMap事件映射;
private final SimpleDataFormat dateFormatter=新SimpleDataFormat(
“dd-MMM-yyyy”);
//当月天数
公共GridCellAdapter(上下文,int textViewResourceId,
整数月,整数年){
超级();
这._context=context;
this.list=new ArrayList();
Log.d(标记“==>月份的传入日期:“+Month+”)
+“年:“+年”;
日历=Calendar.getInstance();
setCurrentDayOfMonth(calendar.get(calendar.DAY/u/u MONTH));
setCurrentWeekDay(calendar.get(calendar.DAY OF the u WEEK));
Log.d(标记“newcalendar:=”+Calendar.getTime().toString());
d(标记“CurrentDayOfWeek:+getCurrentWeekDay());
d(标记“CurrentDayOfMonth:+getCurrentDayOfMonth());
//打印月份
打印月份(月、年);
//查找事件数
eventsPerMonthMap=FindNumber of EventsPermonth(年、月);
}
私有字符串getMontHaString(int i){
返回月份[i];
}
私有字符串getWeekDayAsString(int i){
返回工作日[i];
}
private int getNumberOfDaysOfMonth(int i){
每月返回日[i];
}
公共字符串getItem(int位置){
返回列表。获取(位置);
}
@凌驾
public int getCount(){
返回list.size();
}
/**
*每月印刷
*
*@param-mm
*@param-yy
*/
私人月(整数毫米,整数年){
Log.d(tag,==>printmount:mm:+mm++++yy:+yy);
int trailingSpaces=0;
int daysInPrevMonth=0;
int prevMonth=0;
整年=0;
int nextMonth=0;
int-nextYear=0;
int currentMonth=mm-1;
字符串currentMonthName=GetMonthString(currentMonth);
daysInMonth=getNumberOfDaysOfMonth(当前月);
Log.d(标记“当前月份:”+“”+currentMonthName+“具有”
+daysInMonth+天“);
GregorianCalendar cal=新的GregorianCalendar(yy,当前月份,1);
Log.d(标记“公历:=”+cal.getTime().toString());
如果(当前月份==11){
上月=
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/calendar_button_selector"
android:orientation="vertical" >
<Button
android:id="@+id/calendar_day_gridcell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/calendar_button_selector"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</Button>
<TextView
android:id="@+id/num_events_per_day"
style="@style/calendar_event_style"
android:layout_width="10dip"
android:layout_height="10dip"
android:layout_gravity="right" >
</TextView>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
>
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="800dp">
<FrameLayout
android:layout_width="800dp"
android:layout_height="match_parent"
android:id="@+id/frameLayout">
<ImageButton
android:layout_width="385dp"
android:layout_height="210dp"
android:id="@+id/imageButton"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="@+id/imageView3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Noah Tanenholtz"
android:textSize="28dp"
android:textStyle="bold"
android:textColor="@android:color/black"
android:id="@+id/textView3"
android:layout_alignBottom="@+id/imageView3"
android:layout_toRightOf="@+id/imageView3"
android:layout_toEndOf="@+id/imageView3"
android:layout_gravity="left|bottom" />
<ImageButton
android:layout_width="125dp"
android:layout_height="125dp"
android:id="@+id/imageView3"
android:layout_marginTop="50dp"
android:background= "@mipmap/ic_launcher"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="10dp"
android:layout_gravity="left" />
</FrameLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/lightgray"
android:orientation="vertical"
android:layout_below="@+id/frameLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:id="@+id/buttonlayout"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:background="@color/teal"
android:gravity="left|top"
android:height="60sp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/prevMonth"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10sp"
android:src="@mipmap/back" >
</ImageView>
<TextView
android:id="@+id/currentMonth"
android:layout_width="fill_parent"
android:layout_height="60sp"
android:layout_weight="0.6"
android:gravity="center"
android:text="December"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</TextView>
<ImageView
android:id="@+id/nextMonth"
android:layout_width="50sp"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:layout_marginRight="10sp"
android:src="@mipmap/next" >
</ImageView>
</LinearLayout>
<Button
android:id="@+id/selectedDayMonthYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF" >
</Button>
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/darker_gray">
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Sun"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView6"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="9dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Mon"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView7"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView6"
android:layout_toEndOf="@+id/textView6"
android:layout_marginLeft="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Tue"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView8"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView7"
android:layout_toEndOf="@+id/textView7"
android:layout_marginLeft="24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Wed"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView9"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView8"
android:layout_toEndOf="@+id/textView8"
android:layout_marginLeft="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Thu"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView10"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView9"
android:layout_toEndOf="@+id/textView9"
android:layout_marginLeft="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Fri"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView11"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView10"
android:layout_toEndOf="@+id/textView10"
android:layout_marginLeft="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:text="Sat"
android:textSize="16dp"
android:textStyle="bold"
android:gravity="center"
android:id="@+id/textView12"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textView11"
android:layout_toEndOf="@+id/textView11"
android:layout_marginLeft="30dp" />
</RelativeLayout>
<GridView
android:id="@+id/calendar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="7" >
</GridView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/relativeLayout2">
<ListView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="@+id/listView2"
android:layout_below="@+id/textView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="160dp"
android:layout_height="50dp"
android:text=" Followers:"
android:id="@+id/textView4"
android:textSize="32dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</FrameLayout>
<ListView android:id="@+id/slider_list"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider= "@color/teal"
android:dividerHeight="2dp"
android:background="@android:color/black"
/>
</android.support.v4.widget.DrawerLayout>