【发布时间】:2012-10-08 11:14:12
【问题描述】:
我已经扩展了 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;
}
}
【问题讨论】:
-
if (!dateString.equals(this.prevDate))else 声明添加到此if并在此处隐藏部分...。我仍然认为您应该使用SectionIndexer developer.android.com/reference/android/widget/… -
@Selvin 我会看看
SectionIndexer。似乎更符合逻辑。我还发现我的DateAdapter不是绝对的,会看到最后获取的项目的prevDate。因此,像我一样扩展 SimpleAdapter 不是在不检查相邻项目的情况下就可以工作的。
标签: android listview simpleadapter