0

I am trying to take the value of the description field and I am doing it inside an onBindViewHolder of a Recyclerview adapter to show ipost of the followed users:

enter image description here

This is what I'm trying to do to get the description in the pre-shown structure:

FirebaseFirestore.getInstance().document("/post").collection(listUserId).document("userPosts").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        findFriendsViewHolder.txtDescrizione.setText(documentSnapshot.getString("description"));
                    }
                });

But when I try to run the app (only if the lines of code I just showed are present) the app crashes and the following error is shown in the logcats:

Process: com.conta.pophome, PID: 9348 java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but post has 1

        [... etc]
        at com.conta.pophome.MainActivity$5.onBindViewHolder(MainActivity.java:236)
        at com.conta.pophome.MainActivity$5.onBindViewHolder(MainActivity.java:229)
[... etc]

Is there anyone who could help me resolve this error? I leave you the complete code:

 FirestoreRecyclerOptions<GetInfoPost> options
                = new FirestoreRecyclerOptions.Builder<GetInfoPost>()
                .setQuery(FirebaseFirestore.getInstance().collection("following").document(fAuth.getCurrentUser().getUid()).collection("/userFollowing").limit(1000), GetInfoPost.class)
                .build();

        FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder> firestoreRecyclerAdapter

                =new FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder>(options) {

            @Override
            protected void onBindViewHolder(@NonNull FindFriendsViewHolder findFriendsViewHolder, int i, @NonNull GetInfoPost getInfoPost) {

                final String listUserId = getSnapshots().getSnapshot(i).getId();

                FirebaseFirestore.getInstance().document("/post").collection(listUserId).document("userPosts").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        findFriendsViewHolder.txtDescrizione.setText(documentSnapshot.getString("description"));
                    }
                });



            }



            @NonNull
            @Override
            public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post,parent,false);

                FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
                return viewHolder;

            }
        };

        findPost.setAdapter(firestoreRecyclerAdapter);
        firestoreRecyclerAdapter.startListening();
Conta
  • 236
  • 1
  • 6
  • 21
  • is `post` a document? – Kamal Nayan Jun 19 '21 at 15:26
  • @KamalNayan it is a collection – Conta Jun 19 '21 at 15:37
  • Then why you are using "document("/post")". If it's a collection then you need to use .collection("post") where you are using .document() in query. – Kamal Nayan Jun 19 '21 at 15:39
  • @KamalNayan How should i do? – Conta Jun 19 '21 at 15:41
  • @KamalNayan because after that I would have a QuerySnapshot and not aDocumentSnaphot and so I could not do getString to get the value of the field. So mis I'm asking how – Conta Jun 19 '21 at 15:42
  • https://firebase.google.com/docs/firestore/query-data/get-data#kotlin+ktx_6 have a look here. It's written that in android you can't access subcollection. – Kamal Nayan Jun 19 '21 at 15:49
  • FirebaseFirestore.getInstance().collection("post/"+listUserId+"/userPosts") tried something like this? – Kamal Nayan Jun 19 '21 at 15:51
  • @KamalNayan i was trying it right now but getString () in task.getResult () to fetch field doesn't exist – Conta Jun 19 '21 at 15:52
  • But we are referencing till userPosts and inside that we also have a document....... whose field we need to access – Kamal Nayan Jun 19 '21 at 15:54
  • @KamalNayan I don't understand how I can access for example the description field – Conta Jun 19 '21 at 15:59
  • Do you have access to that document .... android side? – Kamal Nayan Jun 19 '21 at 16:02
  • @KamalNayan What do you mean ? I have to fetch the value of the "description" field but I don't know how to structure the "query" and how to actually fetch it within OnComplete – Conta Jun 19 '21 at 16:04
  • I'm asking that ..... do you have the document id which contains description while querying? – Kamal Nayan Jun 19 '21 at 16:05
  • @KamalNayan No, I don't own it. however the structure I took from the solution of this question https://stackoverflow.com/questions/46979375/firestore-how-to-structure-a-feed-and-follow-system/52153332 – Conta Jun 19 '21 at 16:07
  • @KamalNayan But I just have to take one of the posts (i.e. the first in the collection) so is there a way to take the postid? – Conta Jun 19 '21 at 16:21
  • @KamalNayan That is, to find the last postid I would have to take the name of the last collection. Is there any way to do it? – Conta Jun 19 '21 at 16:26

0 Answers0