1

I'm trying to show all user posts that the user who is using the app follows, and I'm using Firestore. I take all the ids and put them on an arraylist and build a query. I am using FirebaseRecyclerView but I have this error:

2021-06-13 16:33:29.177 22744-22744/com.conta.pophome E/RecyclerView: No adapter attached; skipping layout

I don't think it's an error due to the java code (I'm not fully sure, but I think it is) but I think it's due to the graphics (xml).

graphic code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >


    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/primaryPopHome"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/AppTheme.bar">

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"


                android:scaleType="centerInside"
                android:src="@drawable/poplogo"

                />


            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="300dp"
                android:innerRadius="0dp"
                android:shape="ring"
                android:thicknessRatio="1.9"
                app:cardCornerRadius="80dp">

                <ImageView
                    android:id="@+id/img_profilomain"
                    android:layout_width="42dp"
                    android:layout_height="42dp"

                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:backgroundTint="@color/white"
                    android:scaleType="centerCrop"

                    />
            </androidx.cardview.widget.CardView>


        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:src="@drawable/poplike"
        android:backgroundTint="@color/redButton2"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <!--Floating action button for add alarm-->
    <!--Make sure that you are constraining this
        button to the parent button-->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_alarm_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        app:fabSize="normal"
        app:layout_constraintBottom_toTopOf="@+id/add_fab"
        app:layout_constraintEnd_toEndOf="@+id/add_fab"
        app:layout_constraintStart_toStartOf="@+id/add_fab"
        app:srcCompat="@drawable/ic_baseline_add_24"
        app:borderWidth="0dp"
        android:backgroundTint="@color/redButton2"
        />

    <!--Action name text for the add alarm button-->
    <!--Make sure that you are constraining this Text to
        the add Alarm FAB button-->
    <TextView
        android:id="@+id/add_alarm_action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Crea Post"
        app:layout_constraintBottom_toBottomOf="@+id/add_alarm_fab"
        app:layout_constraintEnd_toStartOf="@+id/add_alarm_fab"
        app:layout_constraintTop_toTopOf="@+id/add_alarm_fab" />

    <!--Floating action button for add person-->
    <!--Make sure that you are constraining this
        button to the add Alarm FAB button-->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_person_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        app:fabSize="normal"
        app:layout_constraintBottom_toTopOf="@+id/add_alarm_fab"
        app:layout_constraintEnd_toEndOf="@+id/add_alarm_fab"
        app:layout_constraintStart_toStartOf="@+id/add_alarm_fab"
        app:srcCompat="@drawable/ic_baseline_search_24"
        app:borderWidth="0dp"
        android:backgroundTint="@color/redButton2"
        />

    <!--Action name text for the add person button-->
    <!--Make sure that you are constraining this Text
        to the add Person FAB button-->
    <TextView
        android:id="@+id/add_person_action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Cerca utenti"
        app:layout_constraintBottom_toBottomOf="@+id/add_person_fab"
        app:layout_constraintEnd_toStartOf="@+id/add_person_fab"
        app:layout_constraintTop_toTopOf="@+id/add_person_fab" />

    <Button
        android:id="@+id/btn_logout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="152dp"
        android:text="Logout"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/appBarLayout"
        app:layout_constraintVertical_bias="0.111" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcViewPostMain"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="56dp"
        app:layout_constraintTop_toBottomOf="@+id/appBarLayout"
        tools:layout_editor_absoluteX="-16dp" />


</androidx.constraintlayout.widget.ConstraintLayout>

and this is the java code:

public class MainActivity extends AppCompatActivity {

    private FirebaseAuth fAuth;
    private DatabaseReference dbRef;
    private DatabaseReference uri;

    private RecyclerView findPost;

    FloatingActionButton mAddFab, mAddAlarmFab, mAddPersonFab;

    // These are taken to make visible and invisible along
    // with FABs
    TextView addAlarmActionText, addPersonActionText;

    // to check whether sub FAB buttons are visible or not.
    Boolean isAllFabsVisible;

    private ArrayList<String> Uidrecord = new ArrayList<>();
    private ArrayList<String> uidFollowing = new ArrayList<>();
    /*
    private String [] Uidrecord = new String[1000];
    private String[] uidFollowing = new String[1000];*/
    int cont =  0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        overridePendingTransition(0, 0);

        findPost = findViewById(R.id.rcViewPostMain);




        fAuth = FirebaseAuth.getInstance();

        // Register all the FABs with their IDs
        // This FAB button is the Parent
        mAddFab = findViewById(R.id.add_fab);
        // FAB button
        mAddAlarmFab = findViewById(R.id.add_alarm_fab);
        mAddPersonFab = findViewById(R.id.add_person_fab);

        // Also register the action name text, of all the FABs.
        addAlarmActionText = findViewById(R.id.add_alarm_action_text);
        addPersonActionText = findViewById(R.id.add_person_action_text);

        // Now set all the FABs and all the action name
        // texts as GONE
        mAddAlarmFab.setVisibility(View.GONE);
        mAddPersonFab.setVisibility(View.GONE);
        addAlarmActionText.setVisibility(View.GONE);
        addPersonActionText.setVisibility(View.GONE);

        // make the boolean variable as false, as all the
        // action name texts and all the sub FABs are invisible
        isAllFabsVisible = false;

        // We will make all the FABs and action name texts
        // visible only when Parent FAB button is clicked So
        // we have to handle the Parent FAB button first, by
        // using setOnClickListener you can see below
        mAddFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!isAllFabsVisible) {

                            // when isAllFabsVisible becomes
                            // true make all the action name
                            // texts and FABs VISIBLE.
                            mAddAlarmFab.show();
                            mAddPersonFab.show();
                            addAlarmActionText.setVisibility(View.VISIBLE);
                            addPersonActionText.setVisibility(View.VISIBLE);

                            // make the boolean variable true as
                            // we have set the sub FABs
                            // visibility to GONE
                            isAllFabsVisible = true;
                        } else {

                            // when isAllFabsVisible becomes
                            // true make all the action name
                            // texts and FABs GONE.
                            mAddAlarmFab.hide();
                            mAddPersonFab.hide();
                            addAlarmActionText.setVisibility(View.GONE);
                            addPersonActionText.setVisibility(View.GONE);

                            // make the boolean variable false
                            // as we have set the sub FABs
                            // visibility to GONE
                            isAllFabsVisible = false;
                        }
                    }
                });

        // below is the sample action to handle add person
        // FAB. Here it shows simple Toast msg. The Toast
        // will be shown only when they are visible and only
        // when user clicks on them
        mAddPersonFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        startActivity(new Intent(MainActivity.this, ricerca.class));
                    }
                });

        // below is the sample action to handle add alarm
        // FAB. Here it shows simple Toast msg The Toast
        // will be shown only when they are visible and only
        // when user clicks on them
        mAddAlarmFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                         startActivity(new Intent(MainActivity.this, posta.class));
                    }
                });





        if(fAuth.getCurrentUser() == null) {
            startActivity(new Intent(MainActivity.this, accesso.class));
            return;
        }

        ImageView img_profilo = findViewById(R.id.img_profilomain);

        dbRef = FirebaseDatabase.getInstance().getReference("Users");
        uri = dbRef.child(fAuth.getCurrentUser().getUid()).child("url");

        Button btn_logout = findViewById(R.id.btn_logout);

        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fAuth.signOut();
                startActivity(new Intent(MainActivity.this, accesso.class));
            }
        });



        showImage();


        FirebaseFirestore.getInstance().collection("following").document(FirebaseAuth.getInstance().getCurrentUser().getUid()).collection("userFollowing").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        document.getData();
                        Uidrecord.add(cont,String.valueOf(document.getData()));
                        cont++;
                    }
                    int conta = 0;
                    for (String item: Uidrecord){
                        if(item == null)
                            break;
                        uidFollowing.add(conta,getFollowingUid(item));
                        Toast.makeText(MainActivity.this, uidFollowing.get(conta), Toast.LENGTH_SHORT).show();
                        conta++;

                    }
                } else {
                    Toast.makeText(MainActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
                }
            }
        });



        findPost.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        onStart();


    }

    @Override
    protected void onStart() {
        super.onStart();

        Query query = FirebaseFirestore.getInstance()
                .collection("post/" + uidFollowing + "/userPosts")
                .limit(1000);


        FirestoreRecyclerOptions<GetInfoPost> options = new FirestoreRecyclerOptions.Builder<GetInfoPost>()
                .setQuery(query, GetInfoPost.class)
                .build();

        FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder> firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<GetInfoPost, FindFriendsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull FindFriendsViewHolder holder, int position, @NonNull GetInfoPost model) {

                holder.txtTitoloInPost.setText(model.getTitle() + " " + model.getValutation() + "/10");
                holder.txtData.setText(model.getdate());
                Picasso.get().load(model.getUrlImage()).into(holder.imgPost);
                holder.txtDescrizione.setText(model.getDescription());
                holder.txtTag.setText(model.getGenre());
            }

            @NonNull
            @Override
            public FindFriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.post, parent, false);

                return new FindFriendsViewHolder(view);
            }
        };

        findPost.setAdapter(firestoreRecyclerAdapter);
        firestoreRecyclerAdapter.startListening();


    }

    public static class FindFriendsViewHolder extends RecyclerView.ViewHolder {

        TextView nameInPost, txtData, txtTag, txtTitoloInPost, txtLikenmb, txtDescrizione;
        ImageView img_profinPost, imgPost, imgLike;
        RelativeLayout cardView;
        GifImageView gifLike;



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

            nameInPost = itemView.findViewById(R.id.nameInPost);
            txtData = itemView.findViewById(R.id.txtData);
            txtTag = itemView.findViewById(R.id.txtTag);
            txtTitoloInPost = itemView.findViewById(R.id.txtTitoloInPost);
            txtLikenmb = itemView.findViewById(R.id.txtLikenmb);
            txtDescrizione = itemView.findViewById(R.id.txtDescrizione);

            img_profinPost = itemView.findViewById(R.id.img_profinPost);
            imgPost = itemView.findViewById(R.id.imgPost);


            cardView = itemView.findViewById(R.id.card_viewR);
            gifLike = itemView.findViewById(R.id.gifLike);

            imgLike = itemView.findViewById(R.id.imgLike);

        }
    }

    @Override
    public void onBackPressed() {
    }


    private void showImage() { //mostro l'immagine usando picasso con l'url nel child url del profilo

        ImageView img_prof = findViewById(R.id.img_profilomain);
        uri.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String url = snapshot.getValue(String.class);
                // Toast.makeText(getApplicationContext(), url, Toast.LENGTH_SHORT).show();
                Picasso.get().load(url).into(img_prof);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }

        });



    }

    private String getFollowingUid(String result){

        int stringLength = result.length();
        String tmp = result.substring(7, stringLength-1);
        return  tmp;
    }
}

I tried to read the other similar questions but could not pinpoint the cause of my problem. Would you help me understand what I'm doing wrong?

Edits: I tried putting the recycler view adapter in onCreate but it still doesn't work -> https://codeshare.io/wndbEB

PopHome2
  • 35
  • 6
  • Try setting the recycler view adapter in `onCreate` instead of `onStart`. – Spidey Jun 13 '21 at 14:55
  • @Spidey just tried, but it doesn't work – PopHome2 Jun 13 '21 at 14:59
  • [check here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android) – androidLearner Jun 13 '21 at 15:06
  • @androidLearner My code should be fine because it is the same but slightly modified that I have used in other acticities for similar purposes and it works there. That's why in my opinion there is a problem in the graphics – PopHome2 Jun 13 '21 at 15:10
  • findPost.setLayoutManager(new LinearLayoutManager(getApplicationContext())); pass "this" instead of getApplicationContext.why did you call onStart() in onCreate. – androidLearner Jun 13 '21 at 15:17
  • @AndroidLearner I tried putting this, this doesn't fix my error – PopHome2 Jun 13 '21 at 15:24

1 Answers1

0

You need to set your recyclerView on the main thread. Try to put the recyclerView in onCreate() and the .startListening() in the onStart.

Marsad
  • 859
  • 1
  • 14
  • 35
franvillacis
  • 199
  • 4