how to list person's post in his profile activity - android

I am building a social app using firebase , when it comes to profile activity and I want to post only person's post I can't ,I managed to post all users post to the home page as news feed but I can't filter only users posts , this is the users node with the person id as a child
i tried this
userRef=FirebaseDatabase.getInstance().getReference().child("Users");
RecieverId=getIntent().getExtras().get("brand_id").toString();
private void DisplayAllOffers() {
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>()
.setQuery(PostRef.child(RecieverId),Posts.class).build();
FirebaseRecyclerAdapter<Posts,PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, PostViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull PostViewHolder holder, int position, #NonNull Posts model) {
// holder.Title.setText(model.getTitle());
final String postKey = getRef(position).getKey();
holder.Price.setText(model.getPrice());
holder.Duration.setText(model.getDuration());
holder.date.setText(model.getDate());
holder.description.setText(model.getDescription());
holder.time.setText(model.getTime());
holder.username.setText(model.getUsername());
holder.setLikeButtonStatus(postKey);
Picasso.get().load(model.getProfileImage()).into(holder.ProfileImage);
Picasso.get().load(model.getPostImage()).into(holder.postImage);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent postIntent = new Intent(BrandActivity.this,ClickPostActivity.class);
postIntent.putExtra("postKey",postKey);
startActivity(postIntent);
}
});
}
#NonNull
#Override
public PostViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view =LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.offer_layout,viewGroup,false);
PostViewHolder holder =new PostViewHolder(view);
return holder;
}
};
Profile_posts.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
and it returns an empty recyclerview

If you want to user-specific post than you have to change your options with
FirebaseRecyclerOptions<Posts> options = new FirebaseRecyclerOptions.Builder<Posts>().setQuery(PostRef.orderByChild("Uid").equalTo(FirebaseAuth.getInstance().getUid())),Posts.class).build();
here this PostRef.orderByChild("Uid").equalTo(FirebaseAuth.getInstance().getUid()) query filter curent user post
you also can get other than the current user specific post, all you have to do only change FirebaseAuth.getInstance().getUid() to user id in equalTo function

Related

Android Firebase - how to get child node data

I have node of "genresComments" that saves comments of each user to for each video id. It goes like that Genre -> video ID -> user ID:Data.
What im trying to get is all the user's id and their data (The data i mean:comment,profilePicture,username).
thats the query that should point to that data
Query query = FirebaseDatabase.getInstance().getReference(Params.GENRESCOMMENTS).child(genre).child(videoID);
But i'm sure that it points only on the "users id" and not bringing me the inside data that i need.
My question is how do i get for each "video id" all his "user id" and his data (comment,profilePicture,username).
EDIT
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
videoID = getArguments().getString(Params.VIDEOID);
genre = getArguments().getString(Params.GENRE);
progressBar.setVisibility(View.VISIBLE);
if(videoID != null){
Query query = FirebaseDatabase.getInstance().getReference(Params.GENRESCOMMENTS).child(genre).child(videoID);
options = new FirebaseRecyclerOptions.Builder<Comment>().setQuery(query,Comment.class).build();
adapter = new FirebaseRecyclerAdapter<Comment,CommentsViewHolder>(options) {
#Override
protected void onBindViewHolder(CommentsViewHolder holder, int position, Comment model) {
holder.userName.setText(model.getUserName());
holder.comment.setText(model.getComment());
Glide.with(getContext()).load(model.getUserProfile()).into(holder.userProfile);
}
#Override
public CommentsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.comment_item,parent,false);
return new CommentsViewHolder(view) ;
}
};
}
if(adapter != null) {
rvComments.setAdapter(adapter);
rvComments.setLayoutManager(new LinearLayoutManager(getContext()));
}
}
I use this query for recycler view
Do like below code (it may get errors): learn about event listener first
FirebaseDatabase.getInstance().getReference(Params.GENRESCOMMENTS).child(genre).child(videoID).addValueEventListener(new ValueEventListener(Datasnapshot snap){
for(DataSnapshot s:snap.getChildren()){
Comment c=s.getValue(Comment.class);
//c.getPRofilePic c.getComment();
}
});
class Comment {
private String comment;
private String profilePic;
private String username;
//getter setter
}

is it possible to combine FirestorRecyclerAdaper with pagination?

I am using FirestoreRecyclerAdaper with the normal form of retrieving and showing data.
my data becomes bigger and I want to make pagination in it, but I didn't find any tutorial to this point. I read documentations but nothing show how to do that because their is only one adapter and 2 queries and FirestorRecyclerAdaper accept only one query.
so is their a solution without changing my code?
final Query query = firebaseFirestore
.collection("Ads")
.orderBy("creationDate", Query.Direction.DESCENDING).limit(5);
query.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
DocumentSnapshot lastVisible = queryDocumentSnapshots
.getDocuments().get(queryDocumentSnapshots.size() - 1);
Query next = firebaseFirestore.collection("Ads")
.orderBy("creationDate")
.startAfter(lastVisible)
.limit(10);
}
});
options = new FirestoreRecyclerOptions
.Builder<MyAdCard>()
.setQuery(query, MyAdCard.class)
.build();
dapter = new FirestoreRecyclerAdapter<MyAdCard, AllCardViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final AllCardViewHolder holder, final int position, #NonNull MyAdCard model) {
holder.publicAd_discription.setText(model.getMyAddiscriptiontxt());
}
#NonNull
#Override
public AllCardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_categoryads, parent, false);
return new AllCardViewHolder(view);
}
};
categoryAdsRV.setAdapter(dapter);
private class AllCardViewHolder extends RecyclerView.ViewHolder {
public AllCardViewHolder(View itemView) {
super(itemView);
}
TextView publicAd_discription = itemView.findViewById(R.id.publicAd_Discription);
}
#Override
protected void onStart() {
super.onStart();
dapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if ( dapter != null ) dapter.stopListening();
}
There is nothing you can do here without changing your code significantly.
There is a FirestorePagingAdapter from Firebase-UI that may help you with the changes you'll need to make.
There is also a sample code I wrote with one example of using Android Architecture Components Paging along with both Firestore and Realtime Database.

How to display data in Firebase RecyclerView UI from Firebase using Id's from Geofire

I have an app I am working on that needs live queries using location from the user. Writing to the geofire node is okay but I have trouble on reading. Once I get the id's how do I make a query with them and display data using the firebase recycleradapter?
Here is my code so far (without Geofire):
FirebaseRecyclerOptions<Venue> options =
new FirebaseRecyclerOptions.Builder<Venue>()
.setQuery(filterRef, Venue.class)
.build();
adapter = new FirebaseRecyclerAdapter<Venue, VenueViewHolder>(options) {
#Override
public VenueViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_venue_item, parent, false);
return new VenueViewHolder(view);
}
#Override
protected void onBindViewHolder(final VenueViewHolder holder, final int position, Venue model) {
final String venueName = model.getVenueName();
holder.venueName.setText(venueName);
Glide.with(holder.mView.getContext())
.load(model.getVenueMainImage())
.apply(new RequestOptions()
.placeholder(R.mipmap.ic_launcher_round))
.into(holder.venueMainImage);
}
};
venuesRecyclerView.setLayoutManager(mLinearLayoutManager);
venuesRecyclerView.setAdapter(adapter);

FirebaseRecyclerAdapter add a hasChild() method?

Here's my database:
Database:{
X:{
JK-KDUKSIDKSIIJDSL1:{
text:"hello",
name:"Donald J. Drunk"
}
JK-KDadDFDDIJDSL1:{
name:"Killery Hlinton"
}
}
}
And I want to filter my FirebaseRecyclerAdapter such that only add to my RecyclerView data that contains the key text. How is this possible? How can I add it? Current code:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
}
You could try overriding the onBindViewHolder method in your FirebaseRecyclerAdapter. Something like this:
mAdapter = new FirebaseRecyclerAdapter<Campaign, CampaignHolder>(Campaign.class, R.layout.recyclerview_template, CampaignHolder.class, ref) {
#Override
public void onBindViewHolder(CampaignHolder viewHolder, int position) {
Campaign model = getItem(position);
if(model.getText() != null){
populateViewHolder(viewHolder, model, position);
}
}
#Override
public void populateViewHolder(final CampaignHolder viewHolder, final Campaign campaign, final int position) {
findViewById(R.id.progress_bar).setVisibility(View.INVISIBLE);
MainActivity.this.holder = viewHolder;
viewHolder.setTitle(campaign.title);
//... Other "set" methods
}
};
With this approach you would still pull all data at the query location but entries without a text attribute just wouldn't be displayed. If that is unsatisfactory and you want to minimize the amount of data you download per query then the only other option I see is closer to what is mentioned in the comments above - meaning you would need to create a separate location that only stores those entries which have a text value.

Updating list when it should be clearing it

I am currently working on inviting clients as guests to use my app. The invitation will be sent by email and their email address will appear in a RecyclerView until they have accepted it. I have made a clear button which will clear all emails within the ListView but for some reason once I selected the clear button, it clears the emails okay, but if I go to input a new email, then all emails previously in the list appear again.
Here is the code used.
public class InviteAdapter extends RecyclerView.Adapter<InviteAdapter.UserViewHolder> {
private static final String TAG = "Invite";
private List <Invites> invites;
public InviteAdapter(List<Invites> invites){
this.invites = invites;
}
#Override
public UserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
final View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row_email_invite, parent, false);
return new UserViewHolder(itemView);
}
#Override
public void onBindViewHolder(UserViewHolder holder, int position) {
Invites invite = invites.get(position);
holder.emailAddress.setText(invite.getEmail_address());
}
Override
public int getItemCount() {
return invites.size();
}
public class UserViewHolder extends RecyclerView.ViewHolder{
TextView emailAddress;
public UserViewHolder(View v) {
super(v);
emailAddress = (TextView) v.findViewById(R.id.row_email);
}
}
public void setInvites(List<Invites> invites){
this.invites = invites;
notifyDataSetChanged();
}
public void delete() {
Log.i(TAG, "Invites deleted");
invites.clear();
notifyDataSetChanged();
}
}
Also have put an image of how the client invite XML looks:
Only clearing your list will not help you get rid of the data as data of your list still exists in your original data source and when you will fetch data from that source again you will get the data you removed from the list. To get rid of that also remove data from your source also.
For eg. If you are creating your list from database then when you clear your list also delete all records from your database too using delete query.

Categories

Resources