Android 将数据绑定库与 CursorAdapter 结合使用
Android use Data Binding Library with CursorAdapter
我想将 Android 数据绑定库与由自定义 CursorAdapter
填充的 ListView
一起使用,但我不知道如何让它工作。我似乎实现了这么简单的事情。
这是我现在拥有的:
public class PlayCursorAdapter extends CursorAdapter {
private List<Play> mPlays;
PlayCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
mPlays = new ArrayList<>();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ListItemPlayBinding binding = ListItemPlayBinding.inflate(LayoutInflater.from(context), parent, false);
Play play = new Play();
binding.setPlay(play);
mPlays.add(play);
return binding.getRoot();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int timeIndex = cursor.getColumnIndexOrThrow(PlayEntry.COLUMN_TIME);
...
long time = cursor.getLong(timeIndex);
...
Play play = mPlays.get(cursor.getPosition());
play.setTime(time);
...
}
}
当前行为:
当我 运行 此代码并在列表中向下滚动时,我在 mPlays 列表中得到一个 IndexOutOfBoundsException
。
期望的行为:
我想使用数据绑定库和 CursorAdapter
用来自 ContentProvider
的数据填充 ListView
。甚至可以将数据绑定库与 CursorAdapter
一起使用吗?或者您是否建议始终使用 RecyclerView
和 RecyclerView.Adapter
?
您应该可以通过删除 mPlays 列表来避免此问题:
public class PlayCursorAdapter extends CursorAdapter {
PlayCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ListItemPlayBinding binding = ListItemPlayBinding.inflate(LayoutInflater.from(context), parent, false);
Play play = new Play();
binding.setPlay(play);
return binding.getRoot();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int timeIndex = cursor.getColumnIndexOrThrow(PlayEntry.COLUMN_TIME);
...
long time = cursor.getLong(timeIndex);
...
ListItemPlayBinding binding = DataBindingUtil.getBinding(view);
Play play = binding.getPlay();
play.setTime(time);
...
}
}
这假设您不只是想在每次 bindView() 时实例化一个新的 Play。
我想将 Android 数据绑定库与由自定义 CursorAdapter
填充的 ListView
一起使用,但我不知道如何让它工作。我似乎实现了这么简单的事情。
这是我现在拥有的:
public class PlayCursorAdapter extends CursorAdapter {
private List<Play> mPlays;
PlayCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
mPlays = new ArrayList<>();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ListItemPlayBinding binding = ListItemPlayBinding.inflate(LayoutInflater.from(context), parent, false);
Play play = new Play();
binding.setPlay(play);
mPlays.add(play);
return binding.getRoot();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int timeIndex = cursor.getColumnIndexOrThrow(PlayEntry.COLUMN_TIME);
...
long time = cursor.getLong(timeIndex);
...
Play play = mPlays.get(cursor.getPosition());
play.setTime(time);
...
}
}
当前行为:
当我 运行 此代码并在列表中向下滚动时,我在 mPlays 列表中得到一个 IndexOutOfBoundsException
。
期望的行为:
我想使用数据绑定库和 CursorAdapter
用来自 ContentProvider
的数据填充 ListView
。甚至可以将数据绑定库与 CursorAdapter
一起使用吗?或者您是否建议始终使用 RecyclerView
和 RecyclerView.Adapter
?
您应该可以通过删除 mPlays 列表来避免此问题:
public class PlayCursorAdapter extends CursorAdapter {
PlayCursorAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
ListItemPlayBinding binding = ListItemPlayBinding.inflate(LayoutInflater.from(context), parent, false);
Play play = new Play();
binding.setPlay(play);
return binding.getRoot();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
int timeIndex = cursor.getColumnIndexOrThrow(PlayEntry.COLUMN_TIME);
...
long time = cursor.getLong(timeIndex);
...
ListItemPlayBinding binding = DataBindingUtil.getBinding(view);
Play play = binding.getPlay();
play.setTime(time);
...
}
}
这假设您不只是想在每次 bindView() 时实例化一个新的 Play。