我有一个绑定到 ListView 的自定义 baseadapter。我正在从自定义类加载对象列表。
当数据更改时,我正在更改某些行的布局以创建标题(是的,我知道我仍然需要修复一些逻辑,但它可以工作)。
我的问题是,当我将列表视图滚动到原来可见的内容之外时,应用程序崩溃并在 HeaderHolder 上出现 ClassCastException 错误(如果我在 catch 处理程序中设置了断点,我会看到该错误)。我认为这是由于视图被破坏和重新创建,不确定。有人可以证实这一点吗?
public class MyCustomBaseAdapter extends BaseAdapter {
private static ArrayList<Appointment> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<Appointment> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public String lastDate = null;
public String thisItemDate = null;
public View getView(int position, View convertView, ViewGroup parent)
{
try
{
thisItemDate = searchArrayList.get(position).GetDTStart().toString().substring(0,10);
if(!thisItemDate.equals(lastDate))
{
HeaderHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.calendar_item_header, null);
holder = new HeaderHolder();
holder.txtHeader = (TextView) convertView.findViewById(R.id.header);
convertView.setTag(holder);
convertView.setPadding(2,2,2,2);
}
else
{
holder = (HeaderHolder) convertView.getTag();
}
holder.txtHeader.setText(thisItemDate);
lastDate = thisItemDate;
return convertView;
}
}
catch (Exception e)
{ //Catch exception if any
System.err.println("Error: " + e.getMessage());
}
ViewHolder holder;
if (convertView == null)
{
convertView = mInflater.inflate(R.layout.calendar_item, null);
holder = new ViewHolder();
holder.txtAttendee = (TextView) convertView.findViewById(R.id.attendee);
holder.txtSummary = (TextView) convertView.findViewById(R.id.summary);
holder.txtStarts = (TextView) convertView.findViewById(R.id.starts);
holder.txtEnds = (TextView) convertView.findViewById(R.id.ends);
convertView.setTag(holder);
convertView.setPadding(4, 4, 4, 16);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtAttendee.setText(searchArrayList.get(position).GetAttendee());
holder.txtSummary.setText(">" + searchArrayList.get(position).GetSummary());
String st = searchArrayList.get(position).GetDTStart().toString();
String en = searchArrayList.get(position).GetDTEnd().toString();
holder.txtStarts.setText(st.substring(0,16));
holder.txtEnds.setText(en.substring(0,16));
return convertView;
}
static class ViewHolder
{
TextView txtAttendee;
TextView txtSummary;
TextView txtStarts;
TextView txtEnds;
}
static class HeaderHolder
{
TextView txtHeader;