android 如何在firebaseRecycler适配器中隐藏当前在线用户数据?

a0zr77ik  于 2023-06-20  发布在  Android
关注(0)|答案(2)|浏览(125)

我的应用程序正在显示firebaseRecycler适配器中的所有firebase数据。我想从显示中排除当前在线用户的数据。例如,Ibad Ullah登录到应用程序,则他的数据不得显示在recyclerview中。我使用了QueryorderByChild,但它隐藏了所有用户数据。我如何才能做到这一点?谢谢!下面是我的代码。

** Jmeter 板活动**

if(fAuth.getCurrentUser() != null){
        UID = FirebaseAuth.getInstance().getCurrentUser().getUid();
    }

        dRef.child(UID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                remainingClicks = String.valueOf(snapshot.child("clicksRemain").getValue(Integer.class));
                showGigCount = String.valueOf(snapshot.child("showGigCount").getValue(Integer.class));
                fiverrLink1 = (String) snapshot.child("gig").getValue();
                String username = (String) snapshot.child("name").getValue();
                clicks.setText(remainingClicks);
                userName.setText(username);
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    recyclerView = findViewById(R.id.dashboardRCV);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
  LoadData();

}

private void LoadData() {
    options = new FirebaseRecyclerOptions.Builder<ModelClass>()
            .setQuery(dRef, ModelClass.class)
            .build();
    adapter = new FirebaseRecyclerAdapter<ModelClass, MyViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {

            if (dRef.child(UID).child("clicksRemain").equals(0)){
                zeroClicks.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
            
            holder.previewLink.setURL(model.getGig(), new URLEmbeddedView.OnLoadURLListener() {
                @Override
                public void onLoadURLCompleted(URLEmbeddedData data) {

                    holder.previewLink.title(data.getTitle());
                    holder.previewLink.description(data.getDescription());
                    holder.previewLink.host(data.getHost());
                    holder.previewLink.thumbnail(data.getThumbnailURL());
                    holder.previewLink.favor(data.getFavorURL());
                }
            });

            //This will hide the gig if its showGigCount becomes 0
            Query query = dRef.orderByChild("showGigCount").equalTo(0);
            ValueEventListener valueEventListener = new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    if (snapshot.exists()){
                        holder.previewLink.setVisibility(View.GONE);
                    }else{
                        holder.previewLink.setVisibility(View.VISIBLE);
                    }
                }
                @Override
                public void onCancelled(@NonNull DatabaseError error) {
                }
            };
            query.addListenerForSingleValueEvent(valueEventListener);

            holder.previewLink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                //    String profileLink = getRef(position).child(model.getGig()).toString();
                    Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(model.getGig()));
                    startActivity(browserIntent);

                }
            });

f0brbegy

f0brbegy1#

没有办法从Firebase数据库结果中排除特定节点,因此常见的方法是在UI中 * 隐藏 * 当前用户的节点:

@Override
protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, @NonNull ModelClass model) {
   if (/* check whether this node is for the current */) { 
      holder.itemView.setVisibility(View.GONE); 
   }
   else {
      holder.itemView.setVisibility(View.VISIBLE); 
   }
   holder.geglink.setText(model.getGig());
}

另见:

您还需要get the key from each node into your Java class,或者您可以保留一个单独的密钥列表。

c2e8gylq

c2e8gylq2#

我做了之前的回答,

holder.itemView.setVisibility(View.GONE);

但这在列表中创建了一个空的空间。所以我接下来做的是:

holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));

这解决了我的问题!
下面是onBindViewHolder函数的完整代码:

if (model.getUid().equals(currentUserID)) {
    holder.itemView.setVisibility(View.GONE);
    holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
else {
    holder.userName.setText(model.getName());
    holder.userStatus.setText(model.getStatus());
}

相关问题