1

What I want to do is loop all the user Posts in the firebase database and show it in recycler view, now each post has a user id, for this I need to write a second query which gets me the username of the particular user's post.

here's how the json looks

what i have done uptill now is this

list = new ArrayList < > ();

newsFeedRecyclerView = (RecyclerView) 
findViewById(R.id.news_feed_recyclerview);

newsFeedRecyclerView.setHasFixedSize(true);
newsFeedRecyclerView.setLayoutManager(new 
LinearLayoutManager(this));
newsFeedListAdapter = new NewsFeedListAdapter(list, 
images);
newsFeedRecyclerView.setAdapter(newsFeedListAdapter);

Query query = databaseReference.child("Posts");
query.addListenerForSingleValueEvent(new 
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {


    for (DataSnapshot data: dataSnapshot.getChildren()) {

        final PostsPOJO postsPOJO = data.getValue(PostsPOJO.class);
        images = postsPOJO.getcontent_post();

        Query userDetails = databaseReference.child("Users/" + postsPOJO.getUserID());


        userDetails.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String username = dataSnapshot.child("Username").getValue(String.class);
                String profilePicturePath = dataSnapshot.child("ProfilePicture").getValue(String.class);

                list.add(new PostsPOJO(postsPOJO.getUserID(), profilePicturePath, username, postsPOJO.getTimestamp(), postsPOJO.getPostText(), postsPOJO.getLocation(), postsPOJO.getcontent_post()));
                newsFeedListAdapter.notifyDataSetChanged();


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    }

}

@Override
public void onCancelled(DatabaseError databaseError) {
   }
});

but the problem here is that it shows all post first whose userid comes first, given the user of second post is different. for e.g( if userid 1 = john and userid 2 = alex, it show all the post of john first and then shows alex's post.

can someone help?

thank you.

Satish Kumar
  • 601
  • 6
  • 14
user233264
  • 13
  • 5

2 Answers2

1

Your approach is very "relational database" oriented. You should not be afraid of denormalizing your data and write several time the same data.

I other words, instead of having UserId under your Post node, just put the Username of your user. Then you will only need to loop once to list the posts with their User Name.

Let's imagine that you want to display the Mobile number together with the Post's Username. You would write the Mobile number under the Post node as well, like you did for the Username. And again, in one loop you get everything you need.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

You are setting a single value event listener and hence when a user click on the post, the code to fetch the posts of that particular user is executed only once.

Instead of this approach, you can use this post to set the onClick of your RecyclerView and write the below code:

  newsFeedRecyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, new RecyclerItemClickListener.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        Log.w(TAG, "You clicked on "+position);
        PostPojo postPojo = (PostPojo) list.get(position);
        String userId = postPojo.getUserId();
        // now is the time to fetch all posts of the above userId

    }
}));