1
  • I am not using any list view in my code, I have used adapter class to make custom calendar. previously, it is working fine.But, as I need to implement background image for any special dates retrieving from database, i wrote code, but now getView() method calling multiple times automatically.
  • please suggest me any solution if anyone knows how to stop it...
  • Thank You.
  • Code is :

    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;
        }
    }    }
    
Hariharan
  • 24,741
  • 6
  • 50
  • 54
  • Method returns `View` so it's called for each row in Adapter widget(s). All you can do is to implement some cache mechanism that will hold child widgets of each row to avoid slow performance and memory leaks (if you are using images). – Simon Dorociak Oct 17 '13 at 12:12
  • getView method will be called days.length times – SKT Oct 17 '13 at 12:24
  • @SKT: When I gave count as 1, still it is calling multiple times – user2890202 Oct 17 '13 at 12:42
  • @H.Moody : Can You please elaborate your answer and Yes.. i need to show background images but it is crashing at that time and giving NullPointerException – user2890202 Oct 17 '13 at 12:43

1 Answers1

0

Quoting android engineer RomainGuy

This is not an issue, there is absolutely no guarantee on the order in which getView() will be called nor how many times.

So the best you can handle is re-using the existing views (row layouts) properly.

Here is another good post.

source: ListView - getView is called too much times

http://developer.android.com/reference/android/widget/GridView.html - as you can see here GridView is some Sort of ListView. It Extends the AbsListView, so i bet the workflow is kinda the same.

Community
  • 1
  • 1
Daniel Bo
  • 2,518
  • 1
  • 18
  • 29
  • Hello Sir.. Thank You For You Ans. but I am not implementing listview as I am implementing Calendar by using gridviews and fragments.. – user2890202 Oct 17 '13 at 13:03