Recycler View not showing any Items With No ERRORS

Hi I am new to android devolpment and I am facing a bug that I could not solve for a week and the BUG is that my recycler view does not show any items in it and I really do not know why I read all the questions on stackoverflow and documantion about it but none helped and I wish you can help me. Actvity:

public class All_Books_Actvity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private BookRecViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_all__books__actvity);
    adapter = new BookRecViewAdapter(this);
    recyclerView = findViewById(R.id.booksRecView);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this ));
    ArrayList<Book> books = new ArrayList<>();
    books.add(new Book(1 , "wimpy kid" , "Jeff kineyy" , 134 , "https://kbimages1-a.akamaihd.net/a93f1391-68be-478b-acb0-63cd7881aed8/1200/1200/False/the-getaway-diary-of-a-wimpy-kid-book-12.jpg" ,
            "asdfasd" , "adfasd"));
    adapter.setBooks(books);
}}

Adapter:

public class BookRecViewAdapter extends RecyclerView.Adapter<BookRecViewAdapter.ViewHolder> {
private static final String TAG = "BookRecViewAdapter";
 ArrayList<Book> books = new ArrayList<>();
private Context mcontexet;
public BookRecViewAdapter(Context mcontexet) {
    this.mcontexet = mcontexet;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout   .list_item_book , parent ,  false);
   return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
    Log.d(TAG, "onBindViewHolder: Working!!!!!");
    holder.txtName.setText(books.get(position).getName());
    Glide.with(mcontexet).asBitmap().load(books.get(position).getImageUrl()).into(holder.imgBook);
    holder.parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(mcontexet, "fads", Toast.LENGTH_SHORT).show();
        }});
    holder.txtDescreption.setText(books.get(position).getShortDesc());
    holder.txtAuthor.setText(books.get(position).getAuthor());
    if (books.get(position).isExpanded()){
        TransitionManager.beginDelayedTransition(holder.parent);
        holder.expandedRealtiveLayout.setVisibility(View.VISIBLE);
        holder.downArrow.setVisibility(View.GONE);
    }else {
        TransitionManager.beginDelayedTransition(holder.parent);
        holder.expandedRealtiveLayout.setVisibility(View.GONE);
       holder.downArrow.setVisibility(View.VISIBLE);
    } }
@Override
public int getItemCount() {
    return books.size();
}
public void setBooks(ArrayList<Book> books) {
    this.books = books;
    notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder{
    private CardView parent;
    private ImageView imgBook;
    private TextView txtName , txtDescreption , txtAuthor ;
    private RelativeLayout expandedRealtiveLayout;
    private ImageView upArrow , downArrow;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        parent = itemView.findViewById(R.id.parent);
        imgBook = itemView.findViewById(R.id.imgBook);
        txtName = itemView.findViewById(R.id.txtBookName);
        txtDescreption = itemView.findViewById(R.id.txtShortDesc);
        expandedRealtiveLayout = itemView.findViewById(R.id.expandedRealtivelayout);
        upArrow = itemView.findViewById(R.id.btnArrowUpp);
        downArrow = itemView.findViewById(R.id.btnArrowDown);
        txtAuthor = itemView.findViewById(R.id.txtAuthor);
        downArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = books.get(getAdapterPosition());
                book.setExpanded(!book.isExpanded());
                notifyItemInserted(getAdapterPosition());
            }
        });
        upArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = books.get(getAdapterPosition());
                book.setExpanded(!book.isExpanded());
                notifyItemInserted(getAdapterPosition());
            }
        });
    }

}

}

Recycler View :

<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=".All_Books_Actvity">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/booksRecView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout> recycler View design:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/parent"
app:cardCornerRadius="7dp"
app:cardElevation="7dp"
android:layout_margin="5dp"
android:layout_centerHorizontal="true"
>
<RelativeLayout
    android:layout_width="200dp"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:id="@+id/expandedRealtivelayout"
        >
        <ImageView
            android:layout_width="130dp"
            android:layout_height="150dp"
            android:id="@+id/imgBook"
            android:visibility="visible"
            android:src="@drawable/ic_launcher_background"
            android:layout_centerHorizontal="true"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Book Name"
            android:layout_below="@+id/imgBook"
            android:textSize="16sp"
            android:textStyle="bold|italic"
            android:textColor="@color/black"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="7dp"
            android:id="@+id/txtBookName"
            android:visibility="visible"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_arrow_down"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="150dp"
            android:id="@+id/btnArrowDown"/>

    </RelativeLayout>
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/expandedRealtivelayout"
        android:visibility="gone">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Author:"
            android:textSize="14sp"
            android:textColor="@color/black"
            android:id="@+id/author"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/author"
            android:text="Name Of Author"
            android:textSize="16sp"
            android:textColor="@color/black"
            android:id="@+id/txtAuthor"
            android:textStyle="italic|bold"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtAuthor"
            android:text="Short Description"
            android:textSize="16sp"
            android:textColor="@color/black"
            android:id="@+id/txtShortDesc"
            android:textStyle="italic|bold"/>
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="40dp"
            android:src="@drawable/ic_arrow_up_right_one"
            android:id="@+id/btnArrowUpp"/>

    </RelativeLayout>
</RelativeLayout>

</androidx.cardview.widget.CardView>

Book class

public class Book {
private int id;
private String name;
private String author;
private int pages;
private String imageUrl;
private String shortDesc;
private String longDesc;
private boolean isExpanded;
public Book(int id, String name, String author, int pages, String imageUrl, String shortDesc, String longDesc) {
    this.id = id;
    this.name = name;
    this.author = author;
    this.pages = pages;
    this.imageUrl = imageUrl;
    this.shortDesc = shortDesc;
    this.longDesc = longDesc;
    this.isExpanded = false;
}

public boolean isExpanded() {
    return isExpanded;
}

public void setExpanded(boolean expanded) {
    isExpanded = expanded;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAuthor() {
    return author;
}

public void setAuthor(String author) {
    this.author = author;
}

public int getPages() {
    return pages;
}

public void setPages(int pages) {
    this.pages = pages;
}

public String getImageUrl() {
    return imageUrl;
}

public void setImageUrl(String imageUrl) {
    this.imageUrl = imageUrl;
}

public String getShortDesc() {
    return shortDesc;
}

public void setShortDesc(String shortDesc) {
    this.shortDesc = shortDesc;
}

public String getLongDesc() {
    return longDesc;
}

public void setLongDesc(String longDesc) {
    this.longDesc = longDesc;
}

@Override
public String toString() {
    return "Book{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", author='" + author + '\'' +
            ", pages=" + pages +
            ", imageUrl='" + imageUrl + '\'' +
            ", shortDesc='" + shortDesc + '\'' +
            ", longDesc='" + longDesc + '\'' +
            '}';
}

}



Read more here: https://stackoverflow.com/questions/65725361/recycler-view-not-showing-any-items-with-no-errors

Content Attribution

This content was originally published by majduddin alboon at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: