Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - view tag isn't correct on view:null (CursorAdapter and DataBinding)

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.

As Androidguys written on their page:

If you are using data binding items inside a ListView or RecyclerView adapter, you may prefer to use:

ListItemBinding binding = ListItemBinding.inflate(layoutInflater, viewGroup, false);
//or
ListItemBinding binding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false);

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));
    }
}

How to correctly implement binding with 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: view tag isn't correct on view: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: at 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 android.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: at android.widget.ListView.correctTooLow(ListView.java:1474) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime: at android.widget.ListView.fillGap(ListView.java:672) 10-14 06:25:43.847 6231-6231/kandrac.xyz.library E/AndroidRuntime: at 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: at 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: at 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: 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 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 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.library E/AndroidRuntime: 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: 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$AsyncInputStage.forward(ViewRootImpl.java:3680) 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$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.847 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: at 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: at 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

like image 351
VizGhar Avatar asked Oct 14 '15 04:10

VizGhar


1 Answers

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:

So the Solution is:

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);
    }

}
like image 93
VizGhar Avatar answered Oct 13 '22 02:10

VizGhar