代码是:
private static final int FIRST_DAY_OF_WEEK = Calendar.MONDAY;
private final Calendar calendar;
public final CalendarItem today;
private final CalendarItem selected;
private final LayoutInflater inflater;
//added
public static CalendarItem eventDate;
//added
private CalendarItem[] days;
Context context;
private List<Events> eventList;
private String dateRetrived;
private String dataRetrived;
private int d;
private int day1;
private String dayS;
private String intMonth;
private String year;
private List<Events> eventList1;
private String dateRetrived1;
private String dataRetrived1;
private Date date1;
private List<Events> dateListInDb;
private String dateOfEvent;
public CalendarAdapter(Context context, Calendar monthCalendar) {
calendar = monthCalendar;
today = new CalendarItem(monthCalendar);
selected = new CalendarItem(monthCalendar);
eventDate = new CalendarItem(monthCalendar);
calendar.set(Calendar.DAY_OF_MONTH, 1);
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
}
public int getCount() {
return days.length;
}
public Object getItem(int position) {
return days[position];
}
public long getItemId(int position) {
final CalendarItem item = days[position];
if (item != null) {
return days[position].id;
}
return -1;
}
@SuppressLint("SimpleDateFormat")
@SuppressWarnings("static-access")
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = inflater.inflate(R.layout.calender_item, null);
}
Toast.makeText(context, "view starts ", Toast.LENGTH_SHORT).show();
final TextView dayView = (TextView)view.findViewById(R.id.date);
final CalendarItem currentItem = days[position];
method();
Toast.makeText(context, "out ", Toast.LENGTH_SHORT).show();
//for now any date... event dates will be taken from DB..
if (currentItem == null) {
dayView.setClickable(false);
dayView.setFocusable(false);
view.setBackgroundDrawable(null);
dayView.setText(null);
} else {
if(currentItem.equals(today)) {
view.setBackgroundResource(R.drawable.today_background);
}
else if (currentItem.equals(selected)) {
view.setBackgroundResource(R.drawable.selected_background);
// Toast.makeText(context, "tapped",Toast.LENGTH_LONG).show();
} else {
view.setBackgroundResource(R.drawable.calnormalmdpi1);
}
dayView.setText(currentItem.text);
}
return view;
}
private void method() {
DatabaseManager db = new DatabaseManager(context);
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
dateListInDb = db.getAllDates();
Iterator<Events> iter = dateListInDb.iterator();
for (int j = 0 ; j < dateListInDb.size() ; j++) {
Toast.makeText(context, "loop starts " + j, Toast.LENGTH_SHORT).show();
Events dateOfEvent = iter.next();
String dateOfEventSingle = dateOfEvent.getDateOfEvent();
try {
date1 = formatter.parse(dateOfEventSingle);
dayS = (String) android.text.format.DateFormat.format("dd", date1);
intMonth = (String) android.text.format.DateFormat.format("MM", date1); //06
year = (String) android.text.format.DateFormat.format("yyyy", date1);
eventDate = new CalendarItem(Integer.parseInt(year), Integer.parseInt(intMonth)-1,Integer.parseInt(dayS));
} catch (ParseException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Toast.makeText(context, "loop ends " + j, Toast.LENGTH_SHORT).show();
//i = false;
}
}
public final void setSelected(int year, int month, int day) {
selected.year = year;
selected.month = month;
selected.day = day;
notifyDataSetChanged();
}
public final void refreshDays() {
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
Toast.makeText(context, "Date : " + month, Toast.LENGTH_SHORT).show();
final int firstDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK);
final int lastDayOfMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
final int blankies;
final CalendarItem[] days;
if (firstDayOfMonth == FIRST_DAY_OF_WEEK) {
blankies = 0;
} else if (firstDayOfMonth < FIRST_DAY_OF_WEEK) {
blankies = Calendar.SATURDAY - (FIRST_DAY_OF_WEEK - 1);
} else {
blankies = firstDayOfMonth - FIRST_DAY_OF_WEEK;
}
days = new CalendarItem[lastDayOfMonth + blankies];
for (int day = 1, position = blankies; position < days.length; position++) {
days[position] = new CalendarItem(year, month, day++);//added dates
}
this.days = days;
notifyDataSetChanged();
}
public static class CalendarItem {
public int year;
public int month;
public int day;
public String text;
public long id;
public CalendarItem(Calendar calendar) {
this(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
}
public CalendarItem(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
this.text = String.valueOf(day);
this.id = Long.valueOf(year + "" + month + "" + day);
}
@Override
public boolean equals(Object o) {
if (o != null && o instanceof CalendarItem) {
final CalendarItem item = (CalendarItem)o;
return item.year == year && item.month == month && item.day == day;
}
return false;
}
} }
答案 0 :(得分:0)
引用安卓工程师RomainGuy
This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.
因此,您可以处理的最好方法是正确地重复使用现有视图(行布局)。
这是另一篇好文章。
来源:ListView - getView is called too much times
http://developer.android.com/reference/android/widget/GridView.html - 正如你在这里看到的,GridView是一些ListView。它扩展了AbsListView,所以我敢打赌工作流程有点相同。