- 我沒有在我的代碼中使用任何列表視圖,我用適配器類來製作自定義日曆。 以前,它工作的很好。但是,因爲我需要實現從數據庫檢索任何特殊日期的背景圖像,我寫了代碼,但現在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; } } }
1
A
回答
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.
所以,你可以處理重新使用現有的意見(行佈局)正確的最好的。
這是另一個好帖子。
來源:ListView - getView is called too much times
http://developer.android.com/reference/android/widget/GridView.html - 你可以看到這裏的GridView是某種類型的ListView。它擴展了AbsListView,所以我敢打賭,工作流程是相同的。
+0
你好先生。謝謝你的答案。但我沒有實現listview,因爲我正在使用gridviews和片段來實現Calendar。 – user2890202
+0
更新了答案:) –
相關問題
- 1. 多次調用Android自定義ArrayAdapter getView方法 - 重置動態TextView值
- 2. 爲什麼ListView的getVIew方法在android中被調用很多次
- 3. 調用getView方法的多次
- 4. 爲什麼getView沒有被調用?
- 5. 何時調用ListView的getView()方法?
- 6. Android getView:爲什麼getView沒有被調用?
- 7. ArrayAdapter的getView()方法在加載和滾動列表視圖時自動調用
- 8. 滾動時調用getView。
- 9. 自定義ArrayAdapter getView未被調用 - 爲什麼不?
- 10. 爲什麼我的自定義getView未被調用?
- 11. getView()方法在適配器中調用mulite時間
- 12. 在EditText焦點事件上自動調用ArrayAdapter的getView()方法
- 13. 什麼是一段時間後自動調用JavaScript函數的正確方法?
- 14. 爲什麼一些委託方法不能自動調用?
- 15. 爲什麼Extjs 4.0.7 TreeStore自動調用http delete方法?
- 16. 的ListView - getView被稱爲太多時間
- 17. GetView調用多次
- 18. Primefaces dataTable多次調用方法時單擊commandButton。爲什麼?
- 19. 爲什麼在請求facelet時多次調用BackingBean方法?
- 20. 爲什麼來自適配器getView(..)方法的條件不適用於ListView
- 21. 滾動ListView時,爲什麼在ListView中不調用onLayout方法?
- 22. 自定義ListView + getView方法
- 23. 當NSArrayController綁定到數組時,爲什麼自動調用add/remove方法?
- 24. 爲什麼在創建JPanel類對象時,會自動調用paintComponent方法?
- 25. 爲什麼touchesMoved方法在拖動視圖時不會被多次調用 - IOS
- 26. 我不明白爲什麼getView()方法無法正確運行時,我重用視圖組件
- 27. 爲什麼在調用方法時使用json.net動態調用RuntimeBinderException
- 28. baseAdapter的getView方法沒有被調用
- 29. ArrayAdapter的getView()方法沒有被調用
- 30. Android Adapter getView方法:調用super.getView或不?
方法返回'View',因此它被調用適配器小部件中的每一行。你所能做的就是實現一些緩存機制,它將保存每一行的子部件,以避免性能下降和內存泄漏(如果你正在使用圖像)。 – Sajmon
getView方法將被稱爲days.length times – SKT
@SKT:當我將count數設爲1時,仍然是多次調用 – user2890202