RecyclerView Not Showing up

Viewed 78

I have some text views and a button to be displayed on top of a RecyclerView. I seem to be doing everything right, but somehow nothing is being displayed. I also checked thru logs and the RecyclerView should have data!

This is not the very first time to display RecyclerViews. However, this is the first time I am displaying a RecyclerView with some elements on top of it.

  1. activity_home.xml - this is the main home page

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/home_RL"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <LinearLayout
        android:id="@+id/top_LL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal">
    
        <EditText
            android:id="@+id/first_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_weight="2"
            android:hint="FirstName" />
    
        <EditText
            android:id="@+id/last_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_weight="2"
            android:hint="LastName" />
    
        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="gone" />
    
        <Button
            android:id="@+id/submit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_weight="1"
            android:onClick="goButton"
            android:text="GO" />
    
        <Button
            android:id="@+id/load"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginEnd="4dp"
            android:layout_weight="1"
            android:onClick="loadButton"
            android:text="L" />
    </LinearLayout>
    
    <android.support.v7.widget.RecyclerView
        android:id="@+id/nameList"
        android:layout_below="@+id/top_LL"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">    
    </android.support.v7.widget.RecyclerView></RelativeLayout>
    
  2. name_list_item.xml

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/list_first_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            tools:text="first name"/>
    
        <TextView
            android:id="@+id/list_last_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            tools:text="second name"/>
    </LinearLayout>
    
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray" />
    

  3. Name Adapter

    public class NameAdapter extends RecyclerView.Adapter {

        private List<Name> mNameList;
        private Context mContext;
    
        public NameAdapter(Context context) {
            mContext = context;
        }
    
        @Override
        public NameViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
            View view = inflater.inflate(R.layout.name_list_item, viewGroup, false);
            return new NameViewHolder(view);
        }
    
        @Override
        public void onBindViewHolder(final NameViewHolder nameViewHolder, int position) {
    
            String firstName = mNameList.get(position).getFirst();
            String lastName = mNameList.get(position).getLast();
    
            nameViewHolder.mListItemFirstName.setText(firstName);
            nameViewHolder.mListItemLastName.setText(lastName);
        }
    
        @Override
        public int getItemCount() {
            if (mNameList == null) {
                return 0;
            }
            return mNameList.size();
        }
    
    
        class NameViewHolder extends RecyclerView.ViewHolder {
    
            TextView mListItemFirstName;
            TextView mListItemLastName;
    
            private NameViewHolder(View itemView) {
                super(itemView);
    
                mListItemFirstName = itemView.findViewById(R.id.list_first_name);
                mListItemLastName = itemView.findViewById(R.id.list_last_name);
            }
    
        }
    
    
    
        public void swapNameList(List<Name> nameList) {
            if (nameList != null) {
                nameList.clear();
            }
            mNameList = nameList;
            this.notifyDataSetChanged();
        }    
    

    }

UPDATE: ISSUE RESOLVED new code should be:

public void swapNameList(List<Name> nameList) {
                if (mNameList != null) {
                    mNameList.clear();
                }
                mNameList = nameList;
                this.notifyDataSetChanged();
            }    
2 Answers

The problem is here

   if (nameList != null) {
        nameList.clear();
    }

you are clearing the list you pass as parameter. This

public void swapNameList(List<Name> nameList) {
    mNameList = nameList;
    this.notifyDataSetChanged();
}

should fix it. (You don't need to clear anything since you are overriding the list every time you call swapNameList)

Solution: The problem here is you are carrying the list forward and clearing it off before assigning to the adapter, hence:

Instead of:

nameList.clear();

Write:

mNameList.clear();

That's all.

Related