我已经扩展了SimpleAdapter
项目的日期分组,这在创建时可以正常工作。列表项目包含分组视图,如果项目属于当前日期组,我的适配器将隐藏此视图:
TextView Title (2012-10-08) - visible
TextView Name (Foo) - visible
TextView Title (2012-10-08) - hidden
TextView Name (Bar) - visible
TextView Title (2012-10-07) - visible
TextView Name (Baz) - visible
TextView Title (2012-10-07) - hidden
TextView Name (Etc) - visible
现在,如果我在完全填充的列表上向下滚动到第二个项目下方的位置,隐藏视图将在再次向上滚动时变得可见。为什么会发生这种情况,我该如何预防?
之后我没有更改适配器,onCreate
因此我不知道为什么我的ListView
或Adapter
更改视图状态。
日期适配器:
public class DateAdapter extends SimpleAdapter
{
protected ArrayList<HashMap<String, String>> items;
private String prevDate = "";
private SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE, dd/MM yyyy", Locale.getDefault());
public DateAdapter (Context context, ArrayList<HashMap<String, String>> items, int resource, String[] from, int[] to)
{
super(context, items, resource, from, to);
this.items = items;
}
@Override
public View getView (int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
try
{
HashMap<String, String> item = this.items.get(position);
Long itemTime = Long.parseLong(item.get("timedate")) * 1000;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(itemTime);
String dateString = dateFormat.format(calendar.getTime());
TextView section = (TextView) view.findViewById(R.id.sectionTitle);
if (!dateString.equals(this.prevDate))
{
this.prevDate = dateString;
section.setText(dateFormat.format(calendar.getTime()));
section.setVisibility(View.VISIBLE);
}
}
catch (Exception e) { Debug.e(e); }
return view;
}
}