问题描述
所以昨天我终于找到了一些时间来尝试新的 DataBinding 库.我曾尝试将它与 CursorAdapter 一起使用,但没有成功.
So yesterday I have finally found some time to try new DataBinding library. I have tried to use it with CursorAdapter but with no success.
正如 Androidguys 在他们的页面上所写:
As Androidguys written on their page:
如果您在 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 个方法.那么如何正确实施呢?大多数情况下,我遇到了一个异常:view 上的 view 标签不正确:null
But in CursorAdapter I have to separate Inflating and Binding into 2 methods. So How correctly implement it? Mostly I got an Exception: view tag isn't correct on view: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 的绑定?
How to correctly implement binding with CursorAdapter?
10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:致命异常:main 10-14 06:25:43.847 6231-6231/kandrac.xyz.library Process E/AndroidRuntime:: kandrac.xyz.library, PID: 6231 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime: java.lang.RuntimeException: view tag 在 view:null 10-14 上不正确06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 kandrac.xyz.library.databinding.BookListItemBinding.bind(BookListItemBinding.java:135) 10-14 06:25:43.81-k62322.xyz.library E/AndroidRuntime:在 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:在 android.widget.AbsListView.obtainView(AbsListView.java:23)-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.widget.ListView.makeAndAddView(ListView.java:1864) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 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/zrac.6231-623库 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:在 android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418) 104:8247:6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.widget.AbsListView.onTouchMove(AbsListView.java:3801) 10-14 06:25:43.847 6231-6231/kandrac.xyz/AndroidRuntime: at E/AndroidRuntimeandroid.widget.AbsListView.onTouchEvent(AbsListView.java:3632) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.View.dispatchTouchEvent(View.java:8471) 10-14 06:25:43.847 6231-6231/kandrac.xyz/AndroidRuntime: E/AndroidRuntime在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime: 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405) 10-14 06:25:43.8147-krac2323.xyz.library E/AndroidRuntime:在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106) 10-14 06:25:43.847 6231-6231/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:在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106) 10:-1425:43.847 6231-6231/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:在 android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405) 106-1: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/AndroidRuntime:在 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: 在 android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405) 10-14 06:25:462318.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:在 com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.app.Activity.dispatchTouchEvent(Activity.java:2742) 10-06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.support.v7.internal.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) 10-14 06:25:46231847/kandrac.xyz.library E/AndroidRuntime:在 com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330) 10-14 06:25:43.847 6231-6231/kandrac.xyz.libraryE/Android 运行时:在 android.view.View.dispatchPointerEvent(View.java:8666) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$ViewPostImeInputView.RootprocessPointerEvent(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-154:036:226231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544) 10-14 06:25:43.847 6231-6231/kandrac.xyz/AndroidRuntimeRuntime:在 android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$InputStage.forwardViewRootImpl.java:3563) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680) 10:254066:43.847 6231-6231/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:在 android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 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:036.81476231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563) 10-14 06:25:43.847 6231-6231/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:在 android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.deliver)java:3544) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library:在 E/AndroidRuntimeandroid.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime: at android.view.ViewRootImpl.enqueueInputEvent(View5Root752.java)10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897) 10-14 06:25:462313177kandrac.xyz.library E/AndroidRuntime:在 android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime:在 android.view.InputEven
推荐答案
基于 Android 开发者关于数据绑定的视频 看起来像 ItemBinding 被设置为视图的标记(在他们的情况下,它在 ViewHolder 内部传递)并稍后检索.那就是我发现在视图中找到绑定的想法的地方:
Based on Android developers video about databinding It looks like ItemBinding is set as tag for view (in their case it is passed inside ViewHolder) and retrieved later. Thats where I found idea to find binding inside views:
所以解决方案是:
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);
}
}
这篇关于Android - view 标签在 view:null 上不正确(CursorAdapter 和 DataBinding)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!