直到现在我还没有在我的项目中找到替换 listview 的解决方案,因为我需要使用与 sqlite 链接的光标。
旧方式如下:
listview.setAdapter(cursorAdapter)
通过这种方式,我可以获取游标来处理数据库中的数据
但现在,recycleview.setAdapter(recycleview.adapter)
它无法识别扩展 BaseAdapter 的适配器
所以任何人都可以帮我一把吗?
直到现在我还没有在我的项目中找到替换 listview 的解决方案,因为我需要使用与 sqlite 链接的光标。
旧方式如下:
listview.setAdapter(cursorAdapter)
通过这种方式,我可以获取游标来处理数据库中的数据
但现在,recycleview.setAdapter(recycleview.adapter)
它无法识别扩展 BaseAdapter 的适配器
所以任何人都可以帮我一把吗?
自己实现其实很简单:
public class CursorAdapter extends RecyclerView.Adapter<ViewHolder>{
Cursor dataCursor;
@Override
public int getItemCount() {
return (dataCursor == null) ? 0 : dataCursor.getCount();
}
public void changeCursor(Cursor cursor) {
Cursor old = swapCursor(cursor);
if (old != null) {
old.close();
}
}
public Cursor swapCursor(Cursor cursor) {
if (dataCursor == cursor) {
return null;
}
Cursor oldCursor = dataCursor;
this.dataCursor = cursor;
if (cursor != null) {
this.notifyDataSetChanged();
}
return oldCursor;
}
private Object getItem(int position) {
dataCursor.moveToPosition(position);
// Load data from dataCursor and return it...
}
}
新的RecyclerView
作品与一个新的RecyclerView.Adapter
基类。所以它不适用于CursorAdapter
.
目前没有可用的默认实现。RecyclerView.Adapter
可能随着正式发布,谷歌会添加它。