- 我没有在我的代码中使用任何列表视图,我使用适配器类来制作自定义日历。以前,它工作正常。但是,由于我需要为从数据库中检索的任何特殊日期实现背景图像,我编写了代码,但现在 getView() 方法会自动调用多次。
- 如果有人知道如何阻止它,请建议我任何解决方案...
- 谢谢你。
代码是:
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; } } }
问问题
227 次
1 回答
0
引用 android 工程师 RomainGuy
This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.
所以你能处理的最好的事情就是正确地重新使用现有的视图(行布局)。
这是另一个好帖子。
http://developer.android.com/reference/android/widget/GridView.html - 如您所见,GridView 是某种 ListView。它扩展了 AbsListView,所以我敢打赌工作流程是一样的。
于 2013-10-17T12:45:26.953 回答