CursorAdapter
が入力されているListView
のAndroidデータバインディングライブラリを使用したいが、それを動作させる方法がわからない。私は達成するのはとても簡単なことです。Androidでデータを使用するライブラリをCursorAdapterで使用する
これは私が今持っているものです。
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
を取得します。
望ましい行動:
私はデータバインディングライブラリを使用してContentProvider
とCursorAdapter
からデータをListView
を移入します。 CursorAdapter
でデータバインディングライブラリを使用することも可能ですか?または、常にRecyclerView
とRecyclerView.Adapter
を使用することをお勧めしますか?
ありがとうございます。このソリューションはうまくいきました。私は 'DataBindingUtil.getBinding(view)'の部分を探していました。あなたは 'CursorAdapter'の代わりに' RecyvlerView.Adapter'を使うことをお勧めしますか?それともこの状況のために過剰ですか?私は 'RecyclerView'を使ってあなたの投稿をMediumで読むことができます。 –
RecyclerViewは、ListViewのほとんどの使用例を処理する新しいウィジェットであり、いくつかの追加機能があります。また、サポートライブラリ内に完全に含まれているため、Androidのバージョン間でも安定性が得られます。だから、将来のレイアウトのためにRecyclerViewを検討する価値があると思います。つまり、何かがうまくいくと変わる理由はありません。 –