4

I'm using RecyclerView with databindings but when I run the app the first time nothing is showing up then after update some content or update the app via instant Run the content appears.

my ViewHolder:

class MyViewHolder extends RecyclerView.ViewHolder {
    private ItemBinding mBinding;

    public MyViewHolder(View itemView) {
        super(itemView);
        mBinding = DataBindingUtil.bind(itemView);
    }

    ItemBinding getBinding() {
        return mBinding;
    }
}

my Adapter:

public class MyAdapter extends FirebaseRecyclerAdapter<MyModel, MyViewHolder> {
    public MyAdapter(Query ref) {
        super(MyModel.class, R.layout.my_item, MyViewHolder.class, ref);
    }

    @Override
    protected void populateViewHolder(MyViewHolder viewHolder, MyModel model, int position) {
        ItemBinding binding = viewHolder.getBinding();
        binding.setMyModel(model);
        binding.executePendingBindings();
    }
}

I found in some other question I need call binding.executePendingBindings() that was I did without success.

Edit

I just added a log call:

Log.d(BuildConfig.TAG, "called populateViewHolder " + position);

on the populateViewHolder method. The log is never printed.

Edit 2

The way how I'm initializing my recyclerView:

// onCreate
LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view)
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(llm);
mRef = FirebaseDatabase.getInstance().getReference().child(CHILD_TREE)    

// onStart    
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
rkmax
  • 17,633
  • 23
  • 91
  • 176
  • That means that the problem is not in the code you shared. Are you sure the user has read permission to the data at `ref`? – Frank van Puffelen Oct 19 '16 at 03:06
  • Yes, I have the permission, I have a version of this code without databindings working without any trouble – rkmax Oct 19 '16 at 03:07
  • Have you checked data? size of arraylist or whatever you are passing? Is it passed correctly in `MyAdapter`? – Ravi Oct 19 '16 at 04:21
  • Yes, I have an observer of the data to show the "empty list text" there I'm checking the size and is greater than 0, what I dont understand is why when I update (just adding a space in the code) with instarun it works – rkmax Oct 19 '16 at 13:25
  • Hey can you share xml layout file? – subrahmanyam boyapati Oct 25 '16 at 05:27
  • Can you try to replace `ItemBinding` with `ViewDataBinding`? – yennsarah Oct 25 '16 at 10:34
  • have your added this Firebase.setAndroidContext(this); – Zar E Ahmer Oct 25 '16 at 12:24
  • @Amylinn I've tried it without success – rkmax Oct 26 '16 at 03:14
  • @Nepster in the new documentation of Firebase they never mention .setAndroidContext I also think is not available, at least not as `Firebase.setAndroidContext` – rkmax Oct 26 '16 at 03:20
  • 1
    Have you already looked at the [firebase/FirebaseUI-Android](https://github.com/firebase/FirebaseUI-Android/issues/279) issue..? Setting the `RecyclerView height` from `wrap_content` to `match_parent` solved the problem there – yennsarah Oct 26 '16 at 10:51
  • @Amylinn you're right the problem was the height – rkmax Oct 27 '16 at 00:33
  • I think I have found your mistake. have a look at my answer now. I have edited it. – Zar E Ahmer Oct 27 '16 at 04:27
  • When retrieving the data from Firebase, add the following line of code at the end of your `onChildAdded` method: `notifyDataSetChanged();` This notifies the recyclerview that a change has been made to your data, and asks it to be updated. Let me know if this helps you. – Michele La Ferla Oct 27 '16 at 07:13
  • I'll add it as an answer :) – yennsarah Oct 27 '16 at 07:19

3 Answers3

1

From my comment:

Have you already looked at the firebase/FirebaseUI-Android issue..? Setting the RecyclerView height from wrap_contentto match_parent solved the problem there.

Apparently, there really is a problem with the height set to wrap_content in the RecyclerView when using the FirebaseRecyclerAdapter with DataBinding.

Change it to

android:layout_height="match_parent"

and it should work.

The FirebaseUI-Android Team thinks that this is a problem with the RecyclerView itself and has closed the issue. This is the crosspost from the Github Issue.

Community
  • 1
  • 1
yennsarah
  • 5,467
  • 2
  • 27
  • 48
-1

Store your data in an ArrayList of your model.Then try implementing the onBindViewHolder method in your adapter. onBindViewHolder has two attributes-holder and position. The holder will be an object of your MyViewHolder class, and you can use the position to get the object of your model at that position from the array list.

sanket pahuja
  • 325
  • 2
  • 12
-1

Edited

You are calling given statements

mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);

in onStart . onStart calls after onCreate. you have set adapter to recyclerView before it's initialized and reference of Database isn't yet created .Put the above two statements in onCreate after

LinearLayoutManager llm = new LinearLayoutManager(this);
llm.setOrientation(LinearLayoutManager.VERTICAL);

mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view)
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(llm);
mRef = FirebaseDatabase.getInstance().getReference().child(CHILD_TREE) ;
// adapter must be set after getting mRef
mAdapter = new MyAdapter(mref.orderByChild("date"));
mRecyclerView.setAdapter(mAdapter);
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
  • Could you please explain the difference between your code and mine – rkmax Oct 26 '16 at 03:29
  • That was my first thought, then I added log.d to onStart and onCreate and always onCreate is called first – rkmax Oct 27 '16 at 04:51
  • have you tried putting the code in onCreate and why do you need to call a method from onStart – Zar E Ahmer Oct 27 '16 at 04:54
  • I use onStart/onStop to attach/detach firebase listeners, in this case the adapter. I think is good practice because I dont need listen for updates when my activity goes to background. and the problem was solved with the @ Amylinn comment. in the databindings version of my code I set the height to wrap_content instead match_parent – rkmax Oct 27 '16 at 04:59