如何检查firebase recylerview是否有值还是在android中为空?

时间:2018-04-18 06:06:04

标签: android firebase firebase-realtime-database firebaseui

我不想向用户显示空的RecyclerView。我知道在recyclerview中有getItemCount方法,但我认为reyclerview可能在单独的线程上执行。因为每当我尝试调用adapter.getitemcount时,即使我的recyclerview具有来自firebase的值,它也会快速计数0。所以我想也许直到值到来之前getItemCount方法执行并返回0或者它可能在单独的线程上执行。但无论哪种方式,即使我的recyclerview具有来自firebase的价值,我也会得到0。所以我想检查我的firebase回收器是否为空,然后相应地更改视图,becoz我不想显示用户和空的recyclerview(一个空活动)。 PLZ建议我该怎么检查呢?顺便说一下我的简单firebase适配器代码:

 allFriendsAdapter = new FirebaseRecyclerAdapter<FriendsModel, ViewHolder>(friendsOptions) {
            @Override
            protected void onBindViewHolder(@NonNull final ViewHolder holder, final int position, @NonNull FriendsModel model) {


                  }

                  @Override
                  public void onCancelled(DatabaseError databaseError) {

                  }
              });


 if(allFriendsAdapter .getItemcount==0){ //this is always returning 0 even when my recyclerview has values

}

2 个答案:

答案 0 :(得分:2)

只需覆盖onDataChanged并在那里检查getItemCount()

答案 1 :(得分:1)

您可以使用 Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged Dim loc As Integer 'check if it contains the <html> tag and if it does select it and change the colour to red. If RichTextBox1.Text.Contains("Card Locked") Then loc = RichTextBox1.Find("Card Locked") RichTextBox1.Select(loc, 12) RichTextBox1.SelectionColor = System.Drawing.Color.Red 'TextBox1.ForeColor = System.Drawing.Color.Red End If If RichTextBox1.Text.Contains("Card Unlocked") Then loc = TextBox1.Find("Card Unlocked") RichTextBox1.Select(loc, 14) RichTextBox1.SelectionColor = System.Drawing.Color.Red End If End sub 检查是否存在任何值:

ValueEventListener

在您的适配器中,您需要使用查询来获取数据,例如上述查询,它还将检查数据是否存在。

然后,您可以通过构建DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users"); Query queries = ref.orderByChild("name").equalTo(name_here); queries.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { Toast.makeText(HomeActivity.this,"data exists",Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(HomeActivity.this,"No data exists",Toast.LENGTH_SHORT).show(); } } @Override public void onCancelled(DatabaseError databaseError) { } });

来配置适配器
FirebaseRecyclerOptions