所以昨天我终于找到了一些时间来尝试新的DataBinding库。我试图将它与CursorAdapter一起使用,但没有成功。
正如Androidguys在他们的页面上写的那样:
如果您在ListView或RecyclerView中使用数据绑定项 适配器,您可能更喜欢使用:
ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false); //or ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);
但是在CursorAdapter中我必须将Inflating和Binding分成2种方法。那么如何正确实现呢?大多数情况下我得到一个异常:视图标签在视图上是不正确的:null
private class BookCursorAdapter extends CursorAdapter {
public BookCursorAdapter(Context context) {
super(context, null, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
BookListItemBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.book_list_item, parent, false);
return binding.getRoot();
// or just
// return getLayoutInflater().inflate(R.layout.book_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// here it fails (and i cannot see source code of bind method)
BookListItemBinding.bind(view).setBook(new Book(cursor));
}
}
如何正确实现与CursorAdapter的绑定?
10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:FATAL EXCEPTION:main 10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:Process :kandrac.xyz.library,PID:6231 10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:java.lang.RuntimeException:视图标签在视图上不正确:null 10 -14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at kandrac.xyz.library.databinding.BookListItemBinding.bind(BookListItemBinding.java:135)10-14 06:25:43.847 6231-6231 /kandrac.xyz.library E / AndroidRuntime:at kandrac.xyz.library.MainActivity $ BookCursorAdapter.bindView(MainActivity.java:95)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.widget.CursorAdapter.getView(CursorAdapter.java:254)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.widget.AbsListView.obtainView(AbsListView.java:2347) )10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at andro id.widget.ListView.makeAndAddView(ListView.java:1864)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.widget.ListView.fillDown(ListView.java:698) 10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.widget.ListView.correctTooLow(ListView.java:1474)10-14 06:25:43.847 6231-6231 / kandrac。 xyz.library E / AndroidRuntime:在android.widget.ListView.fillGap(ListView.java:672)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.widget.AbsListView。 trackMotionScroll(AbsListView.java:4991)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418)10-14 06:25 :43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.widget.AbsListView.onTouchMove(AbsListView.java:3801)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime :在android.widget.AbsListView.onTouchEvent(AbsListView.java:3632)10-14 06: 25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.View.dispatchTouchEvent(View.java:8471)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java :2092)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10-14 06:25:43.847 6231-6231 /kandrac.xyz.library E / AndroidRuntime:在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view .ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)10-14 06:25:43.847 6231-623 1 / kandrac.xyz.library E / AndroidRuntime:在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android。 view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10- 14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)10-14 06:25:43.847 6231-6231 / kandrac.xyz。库E / AndroidRuntime:在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTouchEvent( ViewGroup.java:2106)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)10-14 06:25:43.847 6231- 6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at com。 android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at com.android.internal.policy。 impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.app.Activity.dispatchTouchEvent(Activity.java:2742)10- 14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)10-14 06:25:43.847 6231- 6231 / kandrac.xyz.library E / AndroidRuntime:at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2330)10-14 06:25:43.847 6231-6231 / kandrac.xyz。库E / AndroidRun time:at android.view.View.dispatchPointerEvent(View.java:8666)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl .java:4123)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)10-14 06:25: 43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3544)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3597)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:3563)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3680)10-14 06: 25:43.847 6231-6 231 / kandrac.xyz.library E / AndroidRuntime:在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3571)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3737)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl。 java:3544)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3597)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3563)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime :at android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3571)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ InputStage.deliver( ViewRootImpl.java:3544)10-14 06:25:43.84 7 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752 )10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)10-14 06:25:43.847 6231-6231 /kandrac.xyz.library E / AndroidRuntime:在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view .InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)10-14 06:25:43.847 6231-6231 / kandrac.xyz.library E / AndroidRuntime:at android.view.InputEven
答案 0 :(得分:7)
基于Android developers video about databinding看起来ItemBinding被设置为视图的标记(在它们的情况下它在ViewHolder中传递)并稍后检索。多数民众赞成在那里我找到了在视图中找到绑定的想法:
所以解决方案是:
private class BookCursorAdapter extends CursorAdapter {
...
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
BookListItemBinding binding = BookListItemBinding.inflate(getLayoutInflater(), parent, false);
return binding.getRoot();
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
Book book = new Book(cursor);
BookListItemBinding binding = DataBindingUtil.getBinding(view);
binding.setBook(book);
}
}