gpt4 book ai didi

java - 为什么 getView() 方法会自动调用多次?

转载 作者:行者123 更新时间:2023-12-01 04:16:07 26 4
gpt4 key购买 nike

  • 我没有在代码中使用任何 ListView ,我使用适配器类来制作自定义日历。以前,它工作正常。但是,由于我需要为从数据库检索的任何特殊日期实现背景图像,我编写了代码,但现在 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;
    }
    } }

最佳答案

引用 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.

因此,您能处理的最好方法就是正确地重新使用现有 View (行布局)。

这是另一篇好文章。

来源:ListView - getView is called too much times

http://developer.android.com/reference/android/widget/GridView.html - 正如您在这里所看到的,GridView 是某种 ListView。它扩展了 AbsListView,所以我打赌工作流程有点相同。

关于java - 为什么 getView() 方法会自动调用多次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19426370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com