My app is displaying all the firebase data in firebaseRecycler Adapter. I want to exclude the data of current online user from displaying. e-g Ibad Ullah login to the app then his data must not be displayed in the recyclerview. I used Query orderByChild but it is hiding all users data. How can I achieve this? Thank You. Below is my code.
Dashboard Activity
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);
}
});
There is no way to exclude a specific node from the Firebase database results, so the common way to do this is by hiding the node for the current user in the 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());
}
Also see:
How to hide an item from Recycler View on a particular condition?
You'll also either need to get the key from each node into your Java class, or you can keep a separate list of the keys.
Related
im new to Android Studio. So, im trying to use IF-Else statement to retrive data from firebase using radio button. When click on submit button, it should open a new activity and all of the data should display in one page using listview. But, instead of one page, the output display in many activites. Each activity contain one data. I think its because of my if else statement. Can someone help me? Thank you
So this is my code for SelectAge.class
public class SelectAge extends AppCompatActivity {
Button sbmit;
RadioGroup rg1,rg2;
RadioButton rb1,rb2;
DatabaseReference dr;
EditText et;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_age);
rg1 = findViewById(R.id.A1);
rg2 = findViewById(R.id.A2);
dr = FirebaseDatabase.getInstance().getReference("Data");
sbmit = findViewById(R.id.SubmitBtn);
ArrayList<String> al = new ArrayList<>();
ArrayAdapter<String> ad;
sbmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int radioID1 = rg1.getCheckedRadioButtonId();
int radioID2 = rg2.getCheckedRadioButtonId();
rb1 = findViewById(radioID1);
rb2 = findViewById(radioID2);
dr.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
if (rb1.getText().equals(snapshot.child("Age").getValue()) && (!rb2.getText().equals(snapshot.child("Allergy").getValue()
))) {
String ingredient = snapshot.child("Ingredient").getValue().toString();
Intent intent = new Intent(SelectAge.this, result.class);
intent.putExtra("Result", ingredient);
startActivity(intent);
}
}
#Override
public void onChildChanged(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onChildRemoved(#NonNull DataSnapshot snapshot) {
}
#Override
public void onChildMoved(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
});
}
public void checkButton(View v){
int radioID1 = rg1.getCheckedRadioButtonId();
int radioID2 = rg2.getCheckedRadioButtonId();
rb1 = findViewById(radioID1);
rb2 = findViewById(radioID2);
Toast.makeText(SelectAge.this,"Select" +rb1.getText(),Toast.LENGTH_LONG).show();
}
}
My sample data
In your use case do not use dr.addChildEventListener(new ChildEventListener() {...});
While using a ChildEventListener is the recommended way to read lists of data, there are situations where attaching a ValueEventListener to a list reference is useful.
Attaching a ValueEventListener to a list of data will return the entire list of data as a single DataSnapshot, which you can then loop over to access individual children.
Even when there is only a single match for the query, the snapshot is still a list; it just contains a single item. To access the item, you need to loop over the result:
dr.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
if (rb1.getText().equals(snapshot.child("Age").getValue()) && (!rb2.getText().equals(snapshot.child("Allergy").getValue()
))) {
String ingredient = snapshot.child("Ingredient").getValue().toString();
Intent intent = new Intent(SelectAge.this, result.class);
intent.putExtra("Result", ingredient);
startActivity(intent);
return; // stop looping we have found our item
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadIngredient:onCancelled", databaseError.toException());
// ...
}});
Detailed information in documentation
I hope this helps 😊
I have a large list of posts in a social app. I am using FirebaseRecyclerAdapter that is fetching result directly from firebase and listens to any dataa changes made on firebase realtime database. I dont need all the posts, and when i put some condition on the posts, it shows the relevant posts correctly but those posts that are not fulfilling the condition the view shows at its place. Like its show empty list item if condition is not filled and if condition is fulfilled it show list item with data.
I want to remove listitems from list that doesnt fulfill the condition.
Below is my code. I have a string array that contains the id of business user is following. Post iteam contains a business id, now if post's business id matches with the id of business array post should be shown other wise not.
Query dbQuery = FirebaseDatabase.getInstance().getReference().child("new").child("BusinessPosts").orderByChild("timeStamp");
FirebaseRecyclerOptions<PostMC> options = new FirebaseRecyclerOptions.Builder<PostMC>()
.setQuery(dbQuery, PostMC.class).build();
firebasePostAdapter = new FirebaseRecyclerAdapter<PostMC, PostsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final PostsViewHolder holder, final int position, #NonNull final PostMC postMC) {
if (followingBusinessesList.contains(postMC.getBusinessID())) {
if (source.equals("BPF") || (source.equals("BHF"))) {
sharedByDBRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
holder.shares.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_post_share_colored, 0, 0, 0);
} else {
holder.shares.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_post_share, 0, 0, 0);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
} else {
likedByDBRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
holder.likes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_liked, 0, 0, 0);
} else {
holder.likes.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_like, 0, 0, 0);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
holder.likes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mProcessLike = true;
final int postLikes = Integer.parseInt(String.valueOf(postMC.getPostLikes()));
likedByDBRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (mProcessLike) {
if (dataSnapshot.child(mAuth.getCurrentUser().getUid()).hasChild(postMC.getPostID())) {
businessPostsRef.child(postMC.getPostID()).child("postLikes").setValue("" + (postLikes - 1));
likedByDBRef.child(mAuth.getCurrentUser().getUid()).child(postMC.getPostID()).removeValue();
likedToDBRef.child(postMC.getPostID()).child(mAuth.getCurrentUser().getUid()).removeValue();
holder.likes.setText(postMC.getPostLikes());
mProcessLike = false;
} else {
businessPostsRef.child(postMC.getPostID()).child("postLikes").setValue("" + (postLikes + 1));
likedByDBRef.child(mAuth.getCurrentUser().getUid()).child(postMC.getPostID()).setValue("Liked");
likedToDBRef.child(postMC.getPostID()).child(mAuth.getCurrentUser().getUid()).setValue("Like");
holder.likes.setText(postMC.getPostLikes());
mProcessLike = false;
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
holder.userImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mAuth.getCurrentUser().getUid().equals(postMC.getBusinessID())) {
Intent intent = new Intent(getActivity(), ViewBusinessProfileActivity.class);
intent.putExtra("businessID", postMC.getBusinessID());
startActivity(intent);
}
}
});
holder.profileLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!mAuth.getCurrentUser().getUid().equals(postMC.getBusinessID())) {
Intent intent = new Intent(getActivity(), ViewBusinessProfileActivity.class);
intent.putExtra("businessID", postMC.getBusinessID());
startActivity(intent);
}
}
});
Calendar calendar = Calendar.getInstance(Locale.getDefault());
calendar.setTimeInMillis(Long.parseLong(postMC.getPostTime()));
PrettyTime prettyTime = new PrettyTime(Locale.getDefault());
String ago = prettyTime.format(calendar);
holder.sub.setText(postMC.getPostMessage());
holder.time.setText(ago);
holder.shares.setText(postMC.getPostShares());
holder.likes.setText(postMC.getPostLikes());
holder.main.setText(postMC.getBusinessName());
Glide.with(getActivity()).load(postMC.getBusinessImageUrl()).into(holder.userImage);
if (postMC.getPostImage().equals("")) {
holder.postImage.setVisibility(View.GONE);
} else {
Glide.with(getActivity()).load(postMC.getPostImage()).into(holder.postImage);
}
} else {
}
}
#NonNull
#Override
public PostsViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item_single_post, viewGroup, false);
PostsViewHolder viewHolder = new PostsViewHolder(view);
return viewHolder;
}
};
userPostsRV.setAdapter(firebasePostAdapter);
firebasePostAdapter.startListening();
Businesss Data
Posts Data
Issue Image List Ite
recyclerViewImage
You can hide the entire view in the else part by setting the visibility to "GONE".
In your else part add the following.
if(followingBusinessesList.contains(postMC.getBusinessID())) {
...
}
else{
holder.itemView.setVisibility(View.GONE);
holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
}
I have inserted data and displaying it in List view as I am new to Firebase i dont know how to delete it.
My data format:
Code that i have tried to delete is:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
member.setName(list.get(position));
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String str = member.getName().substring(0,24);
if (str == "") {
Toast.makeText(Retreivedata.this, "plz select record to delete", Toast.LENGTH_LONG).show();
}else {
ref.child("Member").child(str).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
ref.child(str).removeValue();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
Toast.makeText(Retreivedata.this,"Record is deleted",Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),Retreivedata.class);
startActivity(intent);
}
}
Suggest me what to set onclick of delete button.!!
I would suggest using firebase recycler adapter or android recycler view to load your data, but for your case this is what you can do:
Not the best way, but lets say you want to delete an item on click, and assuming that all names are different:
I assumed that member.getName() is giving you the name of the clicked item:
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//ref
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Member");
//Query
Query query = ref.orderByChild("name").equalTo(member.getName());
ValueEventListener listener = new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
//remove
ds.getRef().removeValue();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
};
query.addValueEventListener(listener);
}
});
I'm using the following code to fill a list with data from a child within firebase database. The list is filled successfully, but I've got an issue: FirebaseListAdapter is being called multiple times before stopping, what does not occur when I use it in other activities.
One weird thing is that when I click a specific listView item, data from another item is passed through my openChat intent, what makes wrong data be retrieved to the chat activity I open. It seems that the calling multiple times thing is messing all the data.
Can someone point out what might be wrong with my code and/or what I must do to optimize it?
//database references
chatsRef = FirebaseDatabase.getInstance().getReference().child("chats").child(mAuth.getCurrentUser().getUid()); //the children of this are other users IDs
usersRef = FirebaseDatabase.getInstance().getReference().child("users");
////////// code for populateView I use within FirebaseListAdapter ///////////
FirebaseListAdapter<ChatUsers> firebaseListAdapter = new FirebaseListAdapter<ChatUsers>(getActivity(), ChatUsers.class, R.layout.item_user_listing, chatsRef) {
protected void populateView(View view, ChatUsers chatUsers, int position) {
TextView nameChatItemList = (TextView) view.findViewById(R.id.nameChatItemList);
id_other_user = getRef(position).getKey();
usersRef.child(id_other_user).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//for (DataSnapshot ds : dataSnapshot.getChildren())
Toast.makeText(mActivity, dataSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show();
name = dataSnapshot.child("name").getValue().toString();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent openChat= new Intent(getActivity().getApplicationContext(), Chat.class);
openChat.putExtra("iduser_chat", id_outro_usuario);
openChat.putExtra("name_user_chat", name);
startActivity(openChat);
}
});
}
}
EDIT Changes implemented as suggested by Farmaan
FirebaseListAdapter<ChatUsers> firebaseListAdapter = new FirebaseListAdapter<ChatUsers>(getActivity(), ChatUsers.class, R.layout.item_user_listing, ChatsRef) {
#Override
protected void populateView(View view, ChatUsers chatUsers, int position) {
TextView nomeImageChatItemList = (TextView) view.findViewById(R.id.nomeChatItemList);
String id_other_user = getRef(position).getKey();
usersRef.child(id_other_user).addListenerForSingleValue(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
usersRef.child(id_other_user).addListenerForSingleValue(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
Intent openChat = new Intent(getActivity().getApplicationContext(), Chat.class);
openChat.putExtra("iduser_chat", id_other_user);
openChat.putExtra("nameuser_chat", name);
startActivity(openChat);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
Since id_other_user and name are the class level field and you are updating it every time you populate the view.So the last view which is populated will decide the user id and the name.That's wrong with your code.
chatsRef = FirebaseDatabase.getInstance().getReference().child("chats").child(mAuth.getCurrentUser().getUid()); //the children of this are other users IDs
usersRef = FirebaseDatabase.getInstance().getReference().child("users");
////////// code for populateView I use within FirebaseListAdapter ///////////
protected void populateView (View view, ChatUsuarios chatUsuarios,int position){
TextView nameChatItemList = (TextView) view.findViewById(R.id.nameChatItemList);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String id_other_user = getRef(position).getKey();
usersRef.child(id_other_user).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
//for (DataSnapshot ds : dataSnapshot.getChildren())
String name = dataSnapshot.child("name").getValue().toString();
Intent openChat = new Intent(getActivity().getApplicationContext(), Chat.class);
openChat.putExtra("iduser_chat", id_other_user);
openChat.putExtra("name_user_chat", name);
startActivity(openChat);
Toast.makeText(mActivity, dataSnapshot.getValue().toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
I have to use custom RecyclerView because I don't want to update to list real time.
How do I get an id if I want to go into the details of the data? As in FirebaseRecyclerAdapter.
final String uid = getRef(position).getKey();
I added postId, my posts table, and I wrote the following code. But when click on the image, it goes to the last added image to the list. And when I click upVote, every item goes crazy and they click upVote too.
First, am I on the right track to update the list only when I want to? Second, why is everything going crazy?
PostAdapter
public PostRecyclerAdapter(Context context, Query query) {
this.context = context;
this.query = query;
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
posts.clear();
for (DataSnapshot data : dataSnapshot.getChildren()) {
posts.add(data.getValue(Post.class));
}
Collections.sort(posts, new Comparator<Post>() {
#Override
public int compare(Post o1, Post o2) {
Long a = o1.getCreatedDate();
Long b = o2.getCreatedDate();
if (a < b) {
return -1;
} else if (a == b) {
return 0;
} else {
return 1;
}
}
});
notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
#Override
public void onBindViewHolder(final PostViewHolder viewHolder, int position) {
model = posts.get(position);
postId = model.getPostId();
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(context, model.getImage());
viewHolder.setUpVote(postId);
viewHolder.imvImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, SinglePostActivity.class);
intent.putExtra(Enums.PostKeys.postId.getValue(), postId);
context.startActivity(intent);
}
});
viewHolder.imbUpVote.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!checkAuthUser()) {
context.startActivity(new Intent(context, SignUpActivity.class));
return;
}
processVote = true;
Singleton.getDbPostDownVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostDownVote(postId).child(postId).child(getUserId()).removeValue();
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Singleton.getDbPostUpVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (processVote == true) {
if (dataSnapshot.hasChild(getUserId())) {
Singleton.getDbPostUpVote(postId).child(postId).child(getUserId()).removeValue();
processVote = false;
} else {
Singleton.getDbPostUpVote(postId).child(postId).child(getUserId()).setValue(0);
processVote = false;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
});
}
PostViewHolder:setUpVote
public void setUpVote(final String postId) {
Singleton.getDbPostUpVote(postId).child(postId).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild(getUid())) {
imbUpVote.setImageResource(R.drawable.vote_up_active);
} else {
imbUpVote.setImageResource(R.drawable.vote_up_passive);
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
How do I get an id if I want to go into the details of the data?
Usually the id is a node in your db. As you can see in
final String uid = getRef(position).getKey();
getKey returns tha value of the node in db.
In your case to avoid sorting the list with comparator i would just structure the data like so:
20170111
title : some title
text : some text
20170112
title : some title
text : some text
This way data is going to be sorted by the nodes, which is the date, by Firebase. If you want to be more precise you can also add hours and minutes.
First, am I on the right track to update the list only when I want to?
No.
Calling addValueEventListener() is going to trigger the code inside the listener each time the value in your db changes. In other words, its realtime.
Use addListenerForSingleValueEvent() insted. It fires only once.
Second, why is everything going crazy?
Very important thing about onDataChange() is that it fires not only when the value changes but also the first time you set the listener. That is why everything is getting voted up when you click one item.