我有一長串從SQLite數據庫檢索的對象。爲了簡化,我們假設一個Event
對象具有屬性Date
,Name
和Location
。可擴展ListView按年/月分組項目
我可以使用下面的列表適配器和從Date
分類數據庫中獲取Event
秒的時間Event
的ListView
秒。
public class ImageListButtonListAdapter extends ArrayAdapter<ImageListButtonListItem> {
public ImageListButtonListAdapter(Context c, List<ImageListButtonListItem> items) {
super(c, 0, items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageListButtonView itemView = (ImageListButtonView)convertView;
if (null == itemView)
itemView = ImageListButtonView.inflate(parent);
itemView.setItem(getItem(position));
return itemView;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
}
到目前爲止,這麼好。現在這個列表非常長,我想用ExpandableListView
進一步構建列表,並按月分組Event
項目。我發現創建從BaseExpandableListAdapter
派生的列表適配器的示例通常包括預定義的組和名稱。我只有一大組物品,每個物品都含有(喬達時間)LocalDate
,基於此我可以添加可用於排序的計算屬性YearMonth
。
我的第一個想法是將整個列表傳遞給適配器,並讓適配器確定組,並在創建適配器時將項目分配給組。但恐怕以這種方式處理整個列表,在顯示列表之前創建組並將項目分配給這些組,在已經對性能敏感的視圖中將是一個代價高昂的過程。
我可以想象這是一個相當普遍的情況,並且有一個更加優雅的方法來解決它。最好的方法是什麼?
你能添加代碼爲您的數據模型到的問題也。爲什麼不能在將數據放入ListView之前以正確的方式排序數據? – Darwind
是的,那是我現在的做法。我用棒棒糖和後來的某種方式開始工作。在舊設備上,列表仍然是空的。我將清理一些代碼,並在短時間內將其添加到問題中。謝謝。 – jerry
我用當前的代碼更新了問題。我希望這個片段能夠顯示出足夠的關於數據模型的信息 - 儘量避免發佈過多的代碼。 – jerry