Ali
Ali

Reputation: 2768

RecyclerView.Adapter Issue Binding

Six hours and I cannot figure out what is going on, I have a simplest Adapter and an Activity

Activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="match_parent">    
          <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_stories_comments1"
        android:background="@color/_light_green"
        android:layout_width="match_parent"
        android:layout_height="match_parent"       
        android:padding="0dp"                  
       />
</LinearLayout>

In OnCreate

var RV = FindViewById<AndroidX.RecyclerView.Widget.RecyclerView>(Resource.Id.rv_stories_comments1);
var ad2 = new Stories_Comments_Adapter(comments);                
RV.SetAdapter(ad2);

Stories_Comments_Adapter is also as simple as it gets

internal class Comment_ViewHolder : RecyclerView.ViewHolder
    {
        public TextView username { get; set; }

        public Comment_ViewHolder(View itemView) : base(itemView)
        {
            username = itemView.FindViewById<TextView>(Resource.Id.story_comment_username);
        }
    }

    public class Stories_Comments_Adapter : RecyclerView.Adapter
    {
        private List<CommentItem> comments;

        public Stories_Comments_Adapter(List<CommentItem> comments)
        {
            this.comments = comments;        
        }

        public override int ItemCount => comments.Count;

        public override void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
        {
            Android.Util.Log.Debug("CommentsDebug", "OnBind");
            var Current_Item = comments[position];
            Comment_ViewHolder viewHolder = holder as Comment_ViewHolder;
            viewHolder.username.Text = Current_Item.name;
        }

        public override RecyclerView.ViewHolder OnCreateViewHolder(ViewGroup parent, int viewType)
        {
            Android.Util.Log.Debug("CommentsDebug", "OnCreate");
            LayoutInflater inflater = LayoutInflater.From(parent.Context);
            View itemView = inflater.Inflate(Resource.Layout.item_story_comment, parent, false);
            return new ChatViewHolder(itemView);
        }
    }

Where 'item_story_comment.xml'

<RelativeLayout
    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="wrap_content"
    android:background="?attr/colorBackground"
    android:orientation="vertical">
 <TextView
        android:id="@+id/story_comment_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/demo_about"
        android:textColor="@color/red"        
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center_horizontal"
        android:layout_gravity="center_horizontal"

                    />

</RelativeLayout>

When I RV.SetAdapter(ad2);, it never binds, I never see any data or see the debug messages

I have so many RecyclerViews in this app and all working fine, I feel like I am making some simple or dumb mistake but cannot figure it out what.

Any idea?

Upvotes: 0

Views: 60

Answers (1)

Sdghasemi
Sdghasemi

Reputation: 5598

A RecyclerView needs both Adapter and LayoutManager to show up its items.

Upvotes: 2

Related Questions