2013-01-09 24 views
0

在我的应用程序中,我有要求显示日历。由于日历视图可以从API级别11获得,所以我使用了网格视图。 Basically, I follwed this tutorial。日历正在工作,但日历中的数据出现延迟,意味着,当我点击按钮显示日历时,它会在延迟大约5-10秒后出现。我甚至在禁用日志的情况下进行了测试,我已经将这些日志放在了程序中,但它没有任何区别。我在下面发布我的代码。Android:网格视图中出现延迟的数据

日程表适配器

public class CalendarAdapter extends BaseAdapter //implements OnClickListener 
{ 
static final int FIRST_DAY_OF_WEEK = 0; // Sunday = 0, Monday = 1 

private Context mContext; 

private Calendar month; 
private Calendar selectedDate; 

public String[] days; 

TextView txtCurMonth; 

public static final String TAG = "CalendarAdapter"; 

int curYear, curMonth; 

public ArrayList<EventDetailDAO> mEventDAOList; 

String date, dateToHighlight; 

public static boolean calendarItemClicked = false; 

ViewHolder holder; 

public CalendarAdapter(Context c, Calendar monthCalendar, TextView txtCurMonth) 
{ 
    month = monthCalendar; 
    selectedDate = (Calendar)monthCalendar.clone(); 
    mContext = c; 
    month.set(Calendar.DAY_OF_MONTH, 1); 
    new ArrayList<String>(); 
    this.txtCurMonth = txtCurMonth; 
    mEventDAOList = new ArrayList<EventDetailDAO>(); 
    curYear = selectedDate.get(Calendar.YEAR); 
    curMonth = selectedDate.get(Calendar.MONTH) + 1; 
    refreshDays(); 
}//Constructor 

public void setItems(ArrayList<String> items) 
{ 
    for(int i = 0; i != items.size(); i++) 
    { 
     if(items.get(i).length() == 1) 
     { 
      items.set(i, "0" + items.get(i)); 
     }//if 
    }//for 
}//setItems 


public int getCount() 
{ 
    return days.length; 
}//getCount 

public Object getItem(int position) 
{ 
    return null; 
}//getItem 

public long getItemId(int position) 
{ 
    return 0; 
}//getItemId 

// create a new view for each item referenced by the Adapter 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    if (convertView == null) 
    { 
     holder = new ViewHolder(); 
     convertView = LayoutInflater.from(parent.getContext()) 
         .inflate(R.layout.calendar_grid_adapter, null); 
     holder.linCalAdapParent = (LinearLayout)convertView 
            .findViewById(R.id.linCalAdapParent); 
     holder.dayView = (TextView)convertView.findViewById(R.id.date); 
     convertView.setTag(holder); 
    }//if 
    else 
    { 
     holder = (ViewHolder)convertView.getTag(); 
    }//else 

    Typeface date_TF = Typeface.createFromAsset(mContext.getAssets(), "fonts/arial.ttf"); 
    txtCurMonth.setTypeface(date_TF); 
    txtCurMonth.setText(DateFormat.format("MMMM yyyy", month.getTime())); 
    holder.dayView.setTypeface(date_TF); 
    holder.dayView.setText(days[position]); 

    if(days[position].equals("")) 
    { 
     holder.linCalAdapParent.setBackgroundResource(R.drawable.calendar_tile); 
    }//if 
    else 
    { 
     String monthToCheck = null; 
     if(curMonth < 10) 
     { 
      monthToCheck = "0"+curMonth; 
     }//if 
     else 
     { 
      monthToCheck = Integer.toString(curMonth); 
     }//else 

     dateToHighlight = curYear+"-"+monthToCheck; 
     Log.v(TAG, "date to highlight: "+dateToHighlight); 

     if(isEnabled(position)) 
     { 
      holder.linCalAdapParent.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View v) 
       { 
        Log.v(TAG, "CalendarAdapter mEventDAOList size: "+mEventDAOList.size()); 

        String daysToMatch = days[position]; 

        if(daysToMatch.length() == 1) 
        { 
         daysToMatch = "0"+daysToMatch; 
        }//if 

        Log.v(TAG, "CalendarAdapter day selected: "+daysToMatch); 
        ArrayList<EventDetailDAO> mSelectedEventDAOList 
               = new ArrayList<EventDetailDAO>(); 
        for(int i = 0; i < mEventDAOList.size(); i++) 
        { 
         if(mEventDAOList.get(i).getEvent_Date().contains(daysToMatch)) 
         { 
          mSelectedEventDAOList.add(mEventDAOList.get(i)); 
          selectedDate.set(Calendar.YEAR, curYear); 
          selectedDate.set(Calendar.MONTH, curMonth - 1); 
          selectedDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(daysToMatch)); 
         }//if 
        }//for 

        ((EventsActivity)mContext).getDataFromCalendar(mSelectedEventDAOList, selectedDate); 
        ((EventsActivity)mContext).calInstanceFromAdapter = selectedDate; 
        calendarItemClicked = true; 
       }//onClick 
      }); 
     }//if 
    }//else 
    Log.v(TAG, "Calendar adapter getView called"); 

    return convertView; 
}//getView 

public void refreshDays() 
{ 
    // clear items 
    //items.clear(); 

    int lastDay = month.getActualMaximum(Calendar.DAY_OF_MONTH); 
    int firstDay = (int)month.get(Calendar.DAY_OF_WEEK); 
    Log.v(TAG, "in refreshDays, lastDay: "+lastDay); 
    Log.v(TAG, "in refreshDays, firstDay: "+firstDay); 
    // figure size of the array 
    if(firstDay == 1) 
    { 
     Log.v(TAG, "if first day 1"); 
     days = new String[lastDay + (FIRST_DAY_OF_WEEK * 6)]; 
    }//if 
    else 
    { 
     Log.v(TAG, "else first day not 1"); 
     days = new String[lastDay + firstDay - (FIRST_DAY_OF_WEEK + 1)]; 
    }//else 

    int j=FIRST_DAY_OF_WEEK; 

    // populate empty days before first real day 
    if(firstDay > 1) 
    { 
     Log.v(TAG, "if first day > 1"); 
     for(j = 0; j < firstDay - FIRST_DAY_OF_WEEK; j++) 
     { 
      Log.v(TAG, "in for if first day > 1"); 
      days[j] = ""; 
     }//for 
    }//if 
    else 
    { 
     Log.v(TAG, "else first day < 1"); 
     for(j = 0; j < FIRST_DAY_OF_WEEK * 6; j++) 
     { 
      Log.v(TAG, "in for else first day < 1"); 
      days[j] = ""; 
     }//for 
     j = FIRST_DAY_OF_WEEK * 6 + 1; // sunday => 1, monday => 7 
    }//else 

    // populate days 
    int dayNumber = 1; 
    for(int i = j - 1; i < days.length; i++) 
    { 
     Log.v(TAG, "in for day number"); 
     days[i] = "" + dayNumber; 
     dayNumber++; 
    }//for 
}//refreshDays 

@Override 
public boolean isEnabled(int position) 
{ 
    date = days[position]; 

    if(date.length() == 1) 
    { 
     date = "0"+date; 
    }//if 

    dateToHighlight = dateToHighlight+"-"+date; 
    for(EventDetailDAO mEventDetailDAO: GetEventDetailAsyncTask.mainEventDetailArrayList) 
    { 
     Log.v(TAG, "CalendarAdapter isEnabled dateToHighlight: "+dateToHighlight); 
     if(mEventDetailDAO.getEvent_Date().equals(dateToHighlight)) 
     { 
      mEventDAOList.add(mEventDetailDAO); 
      holder.linCalAdapParent.setBackgroundResource(R.drawable.calendar_tile_sel); 
      return true; 
     }//if 
    }//for 
    return false; 
}//isEnabled 

class ViewHolder 
{ 
    LinearLayout linCalAdapParent; 
    TextView dayView; 
}//ViewHolder 
}//CalendarAdapter 

在我的日历,我必须强调这有一些事件的日期。并且,点击突出显示的日期,它将显示该日期的事件列表。每件事都在起作用。即使我改变月份,日历也会延迟显示。我没有得到原因。

+1

我会说创造了在网格中的每个项的字体可能相当缓慢 – njzk2

回答

1

延迟的可能原因之一可能是调用Typeface.createFromAsset(),这是每个单元所要求的。 createFromAsset()方法消耗大量资源。

为了缓解这种情况,您可以创建一个保留使用字体的单例类,这样字体不会每次都从字体文件生成。或者在onCreate()中初始化它,并将其作为参数传递给CalendarAdapter构造函数,然后在getView()中使用该实例。

(为了检验这一假设,评论说,字体设置为txtCurMonth TextView的线条,并看看是否有差别)

+0

感谢,它工作。从过去的四天里,我一直在嘲笑我的头。 – Nitish