在FirebaseRecyclerAdapter之后,addListenerForSingleValueEvent不起作用

时间:2017-02-24 16:16:56

标签: android firebase recycler-adapter

我正在使用FirebaseRecyclerAdapter,我想在打开活动时显示对话框进度,我希望在加载数据时这会消失。如果列表为空,则会显示一条消息。我已经看到了他们使用addListenerForSingleValueEvent提供解决方案的其他问题。这对我不起作用。我的代码中我做错了什么?你能帮助我吗?谢谢!

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

1 个答案:

答案 0 :(得分:2)

首先显示进度,

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

第二次设置.addListenerForSingleValueEvent并进行空检查。因为如果您的节点下没有包含该路径的数据,dataSnapshot将返回空。

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

最后设置适配器并在检查是否显示后取消进度。因为当有价值时,它不会在if

中输入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);