【问题标题】:How to hide current online user data from displaying in firebaseRecycler Adapter?如何在firebase Recycler Adapter中隐藏当前在线用户数据?
【发布时间】:2022-01-27 18:25:27
【问题描述】:

我的应用在 firebaseRecycler Adapter 中显示所有的 firebase 数据。我想从显示中排除当前在线用户的数据。例如,Ibad Ullah 登录到应用程序,然后他的数据不得显示在回收站视图中。我使用 Query orderByChild 但它隐藏了所有用户数据。我怎样才能做到这一点?谢谢你。下面是我的代码。

仪表板活动

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

                }
            });
    

【问题讨论】:

  • 为了更好地理解,您想从“用户”中获取除已登录的用户之外的所有用户?
  • @AlexMamo 是的,我想隐藏当前在线用户的所有数据。

标签: android firebase-realtime-database android-recyclerview firebaseui


【解决方案1】:

无法从 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,或者您可以保留单独的密钥列表。

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2023-03-25
    • 1970-01-01
    • 2019-01-10
    • 2021-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    相关资源
    最近更新 更多