Nicola Chiari
Nicola Chiari

Reputation: 259

addListenerForSingleValueEvent not work after FirebaseRecyclerAdapter

I'm using FirebaseRecyclerAdapter, I would like to display a dialog progress when I open the activity, and I wish this disappears when the data has been loaded. If the list is empty it will display a message. I've seen other questions in which they gave the solution using addListenerForSingleValueEvent. This doesn't work for me. What am I doing wrong in my code? Can you help me? thank you!

progressDialog = ProgressDialog.show(RecordActivity.this, null, "Loading Data", true);
mDatabaseI = FirebaseDatabase.getInstance().getReference().child("user-record").child(getUid());

mAdapter = new FirebaseRecyclerAdapter<RecordItem, RecordViewHolder>(RecordItem.class, R.layout.record_item, RecordViewHolder.class, mDatabaseI) {
    @Override
    protected void populateViewHolder(final RecordViewHolder viewHolder, final RecordItem model, final int position) {
        final DatabaseReference postRef = getRef(position);

        // Set click listener for the whole post view
        final String postKey = postRef.getKey();

        viewHolder.bindToPost(model);
    }
};

mRecycler.setAdapter(mAdapter);
mDatabaseI.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        //onDataChange called so remove progress bar

        //make a call to dataSnapshot.hasChildren() and based
        //on returned value show/hide empty view

        //use helper method to add an Observer to RecyclerView
        progressDialog.dismiss();
        boolean a = dataSnapshot.hasChildren();

        if (Boolean.TRUE.equals(a)){
            Toast.makeText(getApplicationContext(), "it is empity, Add a item", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

Upvotes: 0

Views: 1515

Answers (1)

AtaerCaner
AtaerCaner

Reputation: 712

First show the progress,

progressDialog = ProgressDialog.show(RecordActivity.this, null, "Loading Data", true);
mDatabaseI = FirebaseDatabase.getInstance().getReference().child("user-record").child(getUid());

Second set the .addListenerForSingleValueEvent and do a null check. Because if there is no data under your node with that path, dataSnapshot will return empty.

mDatabaseI.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if (dataSnapshot.getValue() == null){
           progressDialog.dismiss();
           Toast.makeText(getApplicationContext(), "It is empty, Add an item", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

Finally set the adapter and dismiss the progress after checking if it shown. Because when there is value, it doesn't enter the if in addListenerForSingleValueEvent

mAdapter = new FirebaseRecyclerAdapter<RecordItem, RecordViewHolder>(RecordItem.class, R.layout.record_item, RecordViewHolder.class, mDatabaseI) {
    @Override
    protected void populateViewHolder(final RecordViewHolder viewHolder, final RecordItem model, final int position) {
        if (progressDialog.isShowing()){
          progressDialog.dismiss();
        }
        final DatabaseReference postRef = getRef(position);

        // Set click listener for the whole post view
        final String postKey = postRef.getKey();

        viewHolder.bindToPost(model);
    }
};

mRecycler.setAdapter(mAdapter);

Upvotes: 2

Related Questions