Firestore recyclerview





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















Hi i'm stuck on this part where i have 2 collections first one is users and second one is friends. I want my recyclerview to display my friends on the list with the help of current user because on currentUser other peoples id are stored and i want it to display it on the recyclerview but when it displays it i want to get their information from users where they have their username and email stored.



I want only friends collection ids to display on recyclerview with the help of current user id. Then i want to get values from users collection like their name and email value.



Friends collection
users collection



ADAPTER



public class FriendsAdapter extends FirestoreRecyclerAdapter {



private OnItemClicklistener onItemClicklistener;

public FriendsAdapter(@NonNull FirestoreRecyclerOptions<AllUsers> options) {
super(options);
}

@Override
protected void onBindViewHolder(@NonNull FriendsHolder holder, int position, @NonNull AllUsers model) {
holder.textViewUsername.setText(String.valueOf(model.getName()));
holder.textViewEmail.setText(model.getEmail());
holder.setAvatar(model.getAvatar());

}

@NonNull
@Override
public FriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.all_user_list_display, viewGroup, false);
return new FriendsAdapter.FriendsHolder(view);
}

class FriendsHolder extends RecyclerView.ViewHolder {
TextView textViewUsername;
TextView textViewEmail;
ImageView imageViewAvatar;

public FriendsHolder(@NonNull View itemView) {
super(itemView);

textViewUsername = itemView.findViewById(R.id.all_user_username);
textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && onItemClicklistener != null) {
onItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
}
}
});
}

public void setAvatar(String avatar) {
imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
Picasso.get().load(avatar).into(imageViewAvatar);
}
}

public interface OnItemClicklistener {
void onItemClick(DocumentSnapshot snapshot, int position);
}

public void setOnItemClickListener(FriendsAdapter.OnItemClicklistener listener) {
onItemClicklistener = listener;
}


FriendListFragment



public class FriendsListFragment extends Fragment {

private FirebaseFirestore db;
private CollectionReference usersCollection;
private CollectionReference friendsCollection;
private DocumentReference friends;
private DocumentReference users;
private FirebaseUser current_user;

private FriendsAdapter adapter;
public SearchView search_friends;


public FriendsListFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_friends, container, false);


current_user = FirebaseAuth.getInstance().getCurrentUser();

//FIREBASE
db = FirebaseFirestore.getInstance();
usersCollection = db.collection("users");
friendsCollection = db.collection("friends");
friends = friendsCollection.document(current_user.getUid());

Query friendQuery = usersCollection;

FirestoreRecyclerOptions<AllUsers> options = new FirestoreRecyclerOptions.Builder<AllUsers>()
.setQuery(friendQuery, AllUsers.class)
.build();

adapter = new FriendsAdapter(options);
RecyclerView recyclerView = view.findViewById(R.id.friendsList);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);




return view;
}

@Override
public void onStart() {
super.onStart();
adapter.startListening();
}


AllUsersClass



public class AllUsers {
public String name;
public String avatar;
public String email;
public String userId;

public AllUsers() {
}

public AllUsers(String name, String email, String avatar, String userId) {
this.name = name;
this.email = email;
this.avatar = avatar;
this.userId = userId;
}

public String getName() {
return name;
}

public String getAvatar() {
return avatar;
}

public String getEmail() {
return email;
}

public void setAvatar(String avatar) {
this.avatar = avatar;
}









share|improve this question































    2















    Hi i'm stuck on this part where i have 2 collections first one is users and second one is friends. I want my recyclerview to display my friends on the list with the help of current user because on currentUser other peoples id are stored and i want it to display it on the recyclerview but when it displays it i want to get their information from users where they have their username and email stored.



    I want only friends collection ids to display on recyclerview with the help of current user id. Then i want to get values from users collection like their name and email value.



    Friends collection
    users collection



    ADAPTER



    public class FriendsAdapter extends FirestoreRecyclerAdapter {



    private OnItemClicklistener onItemClicklistener;

    public FriendsAdapter(@NonNull FirestoreRecyclerOptions<AllUsers> options) {
    super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull FriendsHolder holder, int position, @NonNull AllUsers model) {
    holder.textViewUsername.setText(String.valueOf(model.getName()));
    holder.textViewEmail.setText(model.getEmail());
    holder.setAvatar(model.getAvatar());

    }

    @NonNull
    @Override
    public FriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext())
    .inflate(R.layout.all_user_list_display, viewGroup, false);
    return new FriendsAdapter.FriendsHolder(view);
    }

    class FriendsHolder extends RecyclerView.ViewHolder {
    TextView textViewUsername;
    TextView textViewEmail;
    ImageView imageViewAvatar;

    public FriendsHolder(@NonNull View itemView) {
    super(itemView);

    textViewUsername = itemView.findViewById(R.id.all_user_username);
    textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
    itemView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    int position = getAdapterPosition();
    if (position != RecyclerView.NO_POSITION && onItemClicklistener != null) {
    onItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
    }
    }
    });
    }

    public void setAvatar(String avatar) {
    imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
    Picasso.get().load(avatar).into(imageViewAvatar);
    }
    }

    public interface OnItemClicklistener {
    void onItemClick(DocumentSnapshot snapshot, int position);
    }

    public void setOnItemClickListener(FriendsAdapter.OnItemClicklistener listener) {
    onItemClicklistener = listener;
    }


    FriendListFragment



    public class FriendsListFragment extends Fragment {

    private FirebaseFirestore db;
    private CollectionReference usersCollection;
    private CollectionReference friendsCollection;
    private DocumentReference friends;
    private DocumentReference users;
    private FirebaseUser current_user;

    private FriendsAdapter adapter;
    public SearchView search_friends;


    public FriendsListFragment() {
    // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_friends, container, false);


    current_user = FirebaseAuth.getInstance().getCurrentUser();

    //FIREBASE
    db = FirebaseFirestore.getInstance();
    usersCollection = db.collection("users");
    friendsCollection = db.collection("friends");
    friends = friendsCollection.document(current_user.getUid());

    Query friendQuery = usersCollection;

    FirestoreRecyclerOptions<AllUsers> options = new FirestoreRecyclerOptions.Builder<AllUsers>()
    .setQuery(friendQuery, AllUsers.class)
    .build();

    adapter = new FriendsAdapter(options);
    RecyclerView recyclerView = view.findViewById(R.id.friendsList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerView.setAdapter(adapter);




    return view;
    }

    @Override
    public void onStart() {
    super.onStart();
    adapter.startListening();
    }


    AllUsersClass



    public class AllUsers {
    public String name;
    public String avatar;
    public String email;
    public String userId;

    public AllUsers() {
    }

    public AllUsers(String name, String email, String avatar, String userId) {
    this.name = name;
    this.email = email;
    this.avatar = avatar;
    this.userId = userId;
    }

    public String getName() {
    return name;
    }

    public String getAvatar() {
    return avatar;
    }

    public String getEmail() {
    return email;
    }

    public void setAvatar(String avatar) {
    this.avatar = avatar;
    }









    share|improve this question



























      2












      2








      2








      Hi i'm stuck on this part where i have 2 collections first one is users and second one is friends. I want my recyclerview to display my friends on the list with the help of current user because on currentUser other peoples id are stored and i want it to display it on the recyclerview but when it displays it i want to get their information from users where they have their username and email stored.



      I want only friends collection ids to display on recyclerview with the help of current user id. Then i want to get values from users collection like their name and email value.



      Friends collection
      users collection



      ADAPTER



      public class FriendsAdapter extends FirestoreRecyclerAdapter {



      private OnItemClicklistener onItemClicklistener;

      public FriendsAdapter(@NonNull FirestoreRecyclerOptions<AllUsers> options) {
      super(options);
      }

      @Override
      protected void onBindViewHolder(@NonNull FriendsHolder holder, int position, @NonNull AllUsers model) {
      holder.textViewUsername.setText(String.valueOf(model.getName()));
      holder.textViewEmail.setText(model.getEmail());
      holder.setAvatar(model.getAvatar());

      }

      @NonNull
      @Override
      public FriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View view = LayoutInflater.from(viewGroup.getContext())
      .inflate(R.layout.all_user_list_display, viewGroup, false);
      return new FriendsAdapter.FriendsHolder(view);
      }

      class FriendsHolder extends RecyclerView.ViewHolder {
      TextView textViewUsername;
      TextView textViewEmail;
      ImageView imageViewAvatar;

      public FriendsHolder(@NonNull View itemView) {
      super(itemView);

      textViewUsername = itemView.findViewById(R.id.all_user_username);
      textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
      itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      int position = getAdapterPosition();
      if (position != RecyclerView.NO_POSITION && onItemClicklistener != null) {
      onItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
      }
      }
      });
      }

      public void setAvatar(String avatar) {
      imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
      Picasso.get().load(avatar).into(imageViewAvatar);
      }
      }

      public interface OnItemClicklistener {
      void onItemClick(DocumentSnapshot snapshot, int position);
      }

      public void setOnItemClickListener(FriendsAdapter.OnItemClicklistener listener) {
      onItemClicklistener = listener;
      }


      FriendListFragment



      public class FriendsListFragment extends Fragment {

      private FirebaseFirestore db;
      private CollectionReference usersCollection;
      private CollectionReference friendsCollection;
      private DocumentReference friends;
      private DocumentReference users;
      private FirebaseUser current_user;

      private FriendsAdapter adapter;
      public SearchView search_friends;


      public FriendsListFragment() {
      // Required empty public constructor
      }


      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_friends, container, false);


      current_user = FirebaseAuth.getInstance().getCurrentUser();

      //FIREBASE
      db = FirebaseFirestore.getInstance();
      usersCollection = db.collection("users");
      friendsCollection = db.collection("friends");
      friends = friendsCollection.document(current_user.getUid());

      Query friendQuery = usersCollection;

      FirestoreRecyclerOptions<AllUsers> options = new FirestoreRecyclerOptions.Builder<AllUsers>()
      .setQuery(friendQuery, AllUsers.class)
      .build();

      adapter = new FriendsAdapter(options);
      RecyclerView recyclerView = view.findViewById(R.id.friendsList);
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
      recyclerView.setAdapter(adapter);




      return view;
      }

      @Override
      public void onStart() {
      super.onStart();
      adapter.startListening();
      }


      AllUsersClass



      public class AllUsers {
      public String name;
      public String avatar;
      public String email;
      public String userId;

      public AllUsers() {
      }

      public AllUsers(String name, String email, String avatar, String userId) {
      this.name = name;
      this.email = email;
      this.avatar = avatar;
      this.userId = userId;
      }

      public String getName() {
      return name;
      }

      public String getAvatar() {
      return avatar;
      }

      public String getEmail() {
      return email;
      }

      public void setAvatar(String avatar) {
      this.avatar = avatar;
      }









      share|improve this question
















      Hi i'm stuck on this part where i have 2 collections first one is users and second one is friends. I want my recyclerview to display my friends on the list with the help of current user because on currentUser other peoples id are stored and i want it to display it on the recyclerview but when it displays it i want to get their information from users where they have their username and email stored.



      I want only friends collection ids to display on recyclerview with the help of current user id. Then i want to get values from users collection like their name and email value.



      Friends collection
      users collection



      ADAPTER



      public class FriendsAdapter extends FirestoreRecyclerAdapter {



      private OnItemClicklistener onItemClicklistener;

      public FriendsAdapter(@NonNull FirestoreRecyclerOptions<AllUsers> options) {
      super(options);
      }

      @Override
      protected void onBindViewHolder(@NonNull FriendsHolder holder, int position, @NonNull AllUsers model) {
      holder.textViewUsername.setText(String.valueOf(model.getName()));
      holder.textViewEmail.setText(model.getEmail());
      holder.setAvatar(model.getAvatar());

      }

      @NonNull
      @Override
      public FriendsHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
      View view = LayoutInflater.from(viewGroup.getContext())
      .inflate(R.layout.all_user_list_display, viewGroup, false);
      return new FriendsAdapter.FriendsHolder(view);
      }

      class FriendsHolder extends RecyclerView.ViewHolder {
      TextView textViewUsername;
      TextView textViewEmail;
      ImageView imageViewAvatar;

      public FriendsHolder(@NonNull View itemView) {
      super(itemView);

      textViewUsername = itemView.findViewById(R.id.all_user_username);
      textViewEmail = itemView.findViewById(R.id.all_user_userEmail);
      itemView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
      int position = getAdapterPosition();
      if (position != RecyclerView.NO_POSITION && onItemClicklistener != null) {
      onItemClicklistener.onItemClick(getSnapshots().getSnapshot(position), position);
      }
      }
      });
      }

      public void setAvatar(String avatar) {
      imageViewAvatar = (ImageView) itemView.findViewById(R.id.all_user_profile_image);
      Picasso.get().load(avatar).into(imageViewAvatar);
      }
      }

      public interface OnItemClicklistener {
      void onItemClick(DocumentSnapshot snapshot, int position);
      }

      public void setOnItemClickListener(FriendsAdapter.OnItemClicklistener listener) {
      onItemClicklistener = listener;
      }


      FriendListFragment



      public class FriendsListFragment extends Fragment {

      private FirebaseFirestore db;
      private CollectionReference usersCollection;
      private CollectionReference friendsCollection;
      private DocumentReference friends;
      private DocumentReference users;
      private FirebaseUser current_user;

      private FriendsAdapter adapter;
      public SearchView search_friends;


      public FriendsListFragment() {
      // Required empty public constructor
      }


      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_friends, container, false);


      current_user = FirebaseAuth.getInstance().getCurrentUser();

      //FIREBASE
      db = FirebaseFirestore.getInstance();
      usersCollection = db.collection("users");
      friendsCollection = db.collection("friends");
      friends = friendsCollection.document(current_user.getUid());

      Query friendQuery = usersCollection;

      FirestoreRecyclerOptions<AllUsers> options = new FirestoreRecyclerOptions.Builder<AllUsers>()
      .setQuery(friendQuery, AllUsers.class)
      .build();

      adapter = new FriendsAdapter(options);
      RecyclerView recyclerView = view.findViewById(R.id.friendsList);
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
      recyclerView.setAdapter(adapter);




      return view;
      }

      @Override
      public void onStart() {
      super.onStart();
      adapter.startListening();
      }


      AllUsersClass



      public class AllUsers {
      public String name;
      public String avatar;
      public String email;
      public String userId;

      public AllUsers() {
      }

      public AllUsers(String name, String email, String avatar, String userId) {
      this.name = name;
      this.email = email;
      this.avatar = avatar;
      this.userId = userId;
      }

      public String getName() {
      return name;
      }

      public String getAvatar() {
      return avatar;
      }

      public String getEmail() {
      return email;
      }

      public void setAvatar(String avatar) {
      this.avatar = avatar;
      }






      android android-fragments android-recyclerview






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 17:49









      Fantômas

      32.8k156491




      32.8k156491










      asked Nov 26 '18 at 17:21









      Kivanc ÖzmenKivanc Özmen

      135




      135
























          0






          active

          oldest

          votes












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486113%2ffirestore-recyclerview%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53486113%2ffirestore-recyclerview%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen