如何使 firebase 回收器适配器在某些条件下显示特定数据
How to make firebase recycler adapter to show specific data on some conditions
我需要知道如何在特定条件下显示数据,即如果用户的 showGigCount 大于 0 则显示用户的数据。我使用 Query 来实现这一点,但使用它所有数据都消失了。如果标记的节点变为零,则不得显示该特定用户的数据。下面是我的代码。谢谢你们
MyViewHolder.Class
public MyViewHolder(@NonNull @NotNull 查看 itemView) {
超级(项目视图);
previewLink = itemView.findViewById(R.id.uev);
geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
userName = (TextView) itemView.findViewById(R.id.rcvName);
userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
v = itemView;
DashboardActivity.class
fAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference("Users");
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) {
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);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
query.addListenerForSingleValueEvent(valueEventListener);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout, parent, false);
return new MyViewHolder(view);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
display the data of a user if his showGigCount is greater than 0.
为此,请使用以下代码行:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("Users");
Query queryByShowGigCount = usersRef.orderByChild("showGigCount").startAt(0).startAt(1);
options = new FirebaseRecyclerOptions.Builder<ModelClass>()
.setQuery(queryByShowGigCount, ModelClass.class)
.build();
如你所见,解决这个问题的关键是使用Query#startAt()方法。
我需要知道如何在特定条件下显示数据,即如果用户的 showGigCount 大于 0 则显示用户的数据。我使用 Query 来实现这一点,但使用它所有数据都消失了。如果标记的节点变为零,则不得显示该特定用户的数据。下面是我的代码。谢谢你们
MyViewHolder.Class
public MyViewHolder(@NonNull @NotNull 查看 itemView) { 超级(项目视图);
previewLink = itemView.findViewById(R.id.uev);
geglink = (TextView) itemView.findViewById(R.id.rcvGigLink);
userName = (TextView) itemView.findViewById(R.id.rcvName);
userAbout = (TextView) itemView.findViewById(R.id.rcvAbout);
v = itemView;
DashboardActivity.class
fAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
dRef = FirebaseDatabase.getInstance().getReference("Users");
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) {
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);
}
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
};
query.addListenerForSingleValueEvent(valueEventListener);
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout, parent, false);
return new MyViewHolder(view);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
display the data of a user if his showGigCount is greater than 0.
为此,请使用以下代码行:
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = db.child("Users");
Query queryByShowGigCount = usersRef.orderByChild("showGigCount").startAt(0).startAt(1);
options = new FirebaseRecyclerOptions.Builder<ModelClass>()
.setQuery(queryByShowGigCount, ModelClass.class)
.build();
如你所见,解决这个问题的关键是使用Query#startAt()方法。