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

前端 未结 1 1585
粉色の甜心
粉色の甜心 2020-12-18 07:50

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

相关标签:
1条回答
  • 2020-12-18 07:58

    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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题