Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the RecyclerView not working when the compiler isn't giving any errors?

Usually, I don't ask questions like these; I do my research and at least am able to identify the problem before asking on StackOverflow. But this time, even after thoroughly researching and looking over my code over and over again, I can't even identify the problem. So I apologize for asking such a broad question.

I have some RecyclerViews in my app (9, to be exact). One of them is working, eight are not. I have looked over my code over and over again, but I can't seem to identify the problem. I'll paste the code for one recycler view and its adapter. This is what my layout for a single element looks like:

enter image description here

And here is my code for the layout of a single element (I'm just posting it for people to know the ids):

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:background="#0099ff">

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_view"
            app:layout_constraintGuide_percent=".12077"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_view"
            app:layout_constraintGuide_percent=".87923"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_image_view"
            app:layout_constraintGuide_percent=".16908"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_image_view"
            app:layout_constraintGuide_percent=".14706"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_text_view"
            app:layout_constraintGuide_percent=".37198"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_text_view"
            app:layout_constraintGuide_percent=".83"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_text_view_order"
            app:layout_constraintGuide_percent=".13235"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_text_view_order"
            app:layout_constraintGuide_percent=".38235"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_text_view_planter"
            app:layout_constraintGuide_percent=".61765"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_mark_fulfilled_icon"
            app:layout_constraintGuide_percent=".32367"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_mark_fulfilled_icon"
            app:layout_constraintGuide_percent=".43961"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/top_guideline_icon"
            app:layout_constraintGuide_percent=".74265"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/bottom_guideline_icon"
            app:layout_constraintGuide_percent=".96324"
            android:orientation="horizontal"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/left_guideline_message_icon"
            app:layout_constraintGuide_percent=".58696"
            android:orientation="vertical"/>

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/right_guideline_message_icon"
            app:layout_constraintGuide_percent=".70290"
            android:orientation="vertical"/>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/left_border"
            android:background="#0099ff"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="@id/left_guideline_view"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0099ff" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/right_border"
            android:background="#0099ff"
            app:layout_constraintLeft_toLeftOf="@id/right_guideline_view"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#0099ff" />

        </FrameLayout>

        <FrameLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/middle"
            android:paddingTop="8dp"
            android:paddingBottom="8dp"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_view"
            app:layout_constraintRight_toRightOf="@id/right_guideline_view"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/order_planter_message_shape" />

        </FrameLayout>

        <com.mikhaellopez.circularimageview.CircularImageView
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:id="@+id/image_view"
            app:civ_border_color="#ffffff"
            app:civ_border_width="1dp"
            app:civ_shadow="false"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_image_view"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_image_view" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_order"
            android:text="# Trees"
            android:fontFamily="@font/inter_semibold"
            android:textSize="24sp"
            android:textColor="#ffffff"
            android:textAllCaps="false"
            android:gravity="bottom|start"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_text_view"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_text_view"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_text_view_order"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_order"/>

        <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_planter"
            android:text="David Malan, $20"
            android:fontFamily="@font/inter_regular"
            android:textSize="18sp"
            android:textColor="#ffffff"
            android:textAllCaps="false"
            android:gravity="bottom|start"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_text_view"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_text_view"
            app:layout_constraintTop_toTopOf="@+id/bottom_guideline_text_view_order"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_text_view_planter"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/mark_fulfilled_icon"
            android:src="@drawable/ic_check_mark"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_mark_fulfilled_icon"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_mark_fulfilled_icon"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_icon"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_icon"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/message_icon"
            android:src="@drawable/ic_check_mark"
            app:layout_constraintLeft_toLeftOf="@+id/left_guideline_message_icon"
            app:layout_constraintRight_toRightOf="@+id/right_guideline_message_icon"
            app:layout_constraintTop_toTopOf="@+id/top_guideline_icon"
            app:layout_constraintBottom_toBottomOf="@+id/bottom_guideline_icon"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

And here is my code for the adapter of the RecyclerView:

    package com.example.treeapp;

    import android.content.Intent;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import androidx.annotation.NonNull;
    import androidx.recyclerview.widget.RecyclerView;

    import com.mikhaellopez.circularimageview.CircularImageView;

    import java.util.List;

    public class DonorOrdersAdapter extends RecyclerView.Adapter<DonorOrdersAdapter.DonorOrdersViewHolder> {
        public String email;
        public static DownloadImageTask downloadImageTask;

        public static class DonorOrdersViewHolder extends RecyclerView.ViewHolder {
            public CircularImageView imageView;
            public TextView orderTextView;
            public TextView planterTextView;
            public ImageView markFulfilledIcon;
            public ImageView messageIcon;

            DonorOrdersViewHolder(View view) {
                super(view);

                imageView = view.findViewById(R.id.image_view);
                orderTextView = view.findViewById(R.id.text_view_order);
                planterTextView = view.findViewById(R.id.text_view_planter);
                markFulfilledIcon = view.findViewById(R.id.mark_fulfilled_icon);
                messageIcon = view.findViewById(R.id.message_icon);
                downloadImageTask = new DownloadImageTask(imageView);
            }
        }

        DonorOrdersAdapter(String email) {
            this.email = email;
        }

        List<Integer> orders = MainActivity.database.ordersDao().getOrdersByDonor(email);

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

            return new DonorOrdersViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull DonorOrdersViewHolder holder, int position) {
            final int current = orders.get(position);
            final String planterEmail = MainActivity.database.ordersDao().getPlanter(current);

            if (MainActivity.database.plantersDao().getImageUrl(planterEmail) != null) {
                downloadImageTask.execute(MainActivity.database.plantersDao().getImageUrl(planterEmail));
            }

            holder.orderTextView.setText(MainActivity.database.ordersDao(  ).getTrees(current) + " trees");
            holder.planterTextView.setText(MainActivity.database.plantersDao().getName(planterEmail) + ", $" +
                                           MainActivity.database.ordersDao().getPayment(current));

            holder.markFulfilledIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent fulfillIntent = new Intent(view.getContext(), ConfirmPaymentActivity.class);
                    fulfillIntent.putExtra("email", email);
                    fulfillIntent.putExtra("orderId", current);

                    view.getContext().startActivity(fulfillIntent);
                }
            });

            holder.messageIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent messageIntent = new Intent(view.getContext(), MessageActivity.class);
                    messageIntent.putExtra("email", email);
                    messageIntent.putExtra("peerEmail", planterEmail);
                    messageIntent.putExtra("archived", 0);

                    view.getContext().startActivity(messageIntent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return orders.size();
        }
    }

I am using a SQLite table using the room library. Here is the code for a row of the table:

    package com.example.treeapp;

    import androidx.room.ColumnInfo;
    import androidx.room.Entity;
    import androidx.room.PrimaryKey;

    @Entity(tableName = "Orders")
    public class Order {
        @PrimaryKey(autoGenerate = true)
        public int id;

        @ColumnInfo(name = "Donor")
        public String donorEmail;

        @ColumnInfo(name = "Planter")
        public String planterEmail;

        @ColumnInfo(name = "Trees")
        public int trees;

        @ColumnInfo(name = "Payment")
        public int payment;

        @ColumnInfo(name = "Fulfilled")
        public int fulfilled;
    }

And here is the code for the Data Access Object:

    package com.example.treeapp;

    import androidx.room.Dao;
    import androidx.room.Query;

    import java.util.List;

    @Dao
    public interface OrdersDao {
        @Query("INSERT INTO Orders (Donor, Planter, Trees, Payment, Fulfilled) VALUES (:donorEmail, :planterEmail, :trees, :payment, 0)")
        void create(String donorEmail, String planterEmail, int trees, int payment);

        @Query("SELECT Donor FROM Orders WHERE id = :id")
        String getDonor(int id);

        @Query("SELECT Planter FROM Orders WHERE id = :id")
        String getPlanter(int id);

        @Query("SELECT Trees FROM Orders WHERE id = :id")
        int getTrees(int id);

        @Query("SELECT Payment FROM Orders WHERE id = :id")
        int getPayment(int id);

        @Query("SELECT Fulfilled FROM Orders WHERE id = :id")
        int getFulfilled(int id);

        @Query("UPDATE Orders SET Fulfilled = 1 WHERE id = :id")
        void markFulfilled(int id);

        @Query("SELECT id FROM Orders WHERE Donor = :donorEmail AND Fulfilled = 0")
        List<Integer> getOrdersByDonor(String donorEmail);

        @Query("SELECT id FROM Orders WHERE Planter = :planterEmail AND Fulfilled = 0")
        List<Integer> getOrdersToPlanter(String planterEmail);

        @Query("SELECT id FROM Orders WHERE Fulfilled = 0")
        List<Integer> getUnfulfilledOrders();
    }

And here is my code for the activity in which the recycler views are used:

    package com.example.treeapp;

    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;

    import android.os.Bundle;

    public class DonorOrdersActivity extends AppCompatActivity {
        private String email;
        private RecyclerView recyclerView;
        private RecyclerView.Adapter adapter;
        private RecyclerView.LayoutManager layoutManager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_donor_orders);

            email = getIntent().getStringExtra("email");
            recyclerView = findViewById(R.id.recycler_view);
            adapter = new DonorOrdersAdapter(email);
            layoutManager = new LinearLayoutManager(this);

            recyclerView.setAdapter(adapter);
            recyclerView.setLayoutManager(layoutManager);
        }
    }

If anyone is able to figure out what's wrong with my RecyclerViews, please tell me.

**EDIT: ** I have tested that my database actually contains the orders by logging data from the orders table.

like image 262
Huzaifa Avatar asked Jan 24 '23 19:01

Huzaifa


2 Answers

If you see your logcat, you can see E/RecyclerView: No adapter attached; skipping layout. So the solution is change this :

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);

to this :

recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
like image 67
zihadrizkyef Avatar answered May 19 '23 10:05

zihadrizkyef


If getItemCount() returns zero RV wont inflate any viewHolders. Usually if nothing is shown in Rv, putting breakpoints or logging will help identifying the issue.

like image 34
Rinat Diushenov Avatar answered May 19 '23 10:05

Rinat Diushenov