Recycler View Not Showing Anything

16,969

The code which i have written was perfectly good and its working without any problem, In my main code the RecyclearView height was zero that's why i didn't get the output .

Share:
16,969
Sushin Pv
Author by

Sushin Pv

upGrad Jeet Responsible for Technical Product Development, Architecture Design, Product Performance and Scalability at upGrad Jeet, which includes both frontend and backend Self created the entire initial version of upGrad Jeet and The Gate Academy LMS which has mutiple backend services and web application Leading a team of more than 15 developers Funloop Formally WhatsCut Pro Responsible for Technical Product Development, Architecture Design, Server Configuration, Backend Design and development, Product Performance and Scalability at WhatsCut Pro Technical Working with NextJs and building high performance application Designed multiple architecture patterns for both front-end and backend, Proficient in MongoDB, Express JS, Angular and Node JS, PHP, ReactJS and Android. Other Achievements An active member of FSMK ( Free Software Movement Of Karnataka ) Led the GLUG (Glu Linux Users Group) initiative for PA College of Engineering - GLUG PACE . Secured Best Engineering Student Innovative Project Award in S.D.M IT - State Level Project Competition and Exhibition. Secured first prize in technical paper presentation - Techprints 5.0 - A National Level Technical Paper Presentation at St. Joseph Engineering college for MOZHI Android application. Part of more than 40 workshops and self hosted more than 15 workshops

Updated on June 12, 2022

Comments

  • Sushin Pv
    Sushin Pv almost 2 years

    I have been making an app that uses a RecyclerView but its not showing any thing..why contents of the recycler view have not been showing up.my codes are bellow

    item_contact.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:paddingBottom="10dp"
        android:paddingTop="10dp">
    
        <TextView
            android:id="@+id/contact_name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
        <Button
            android:id="@+id/message_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:textSize="10sp" />
    </LinearLayout>
    

    ContactsAdaptor.java

    import android.content.Context;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.TextView;
    
    import java.util.List;
    public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
    
        private List<Contact> mContacts;
        public ContactsAdapter(List<Contact> contacts) {
            mContacts = contacts;
        }
    
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Context context = parent.getContext();
            LayoutInflater inflater = LayoutInflater.from(context);
            View contactView = inflater.inflate(R.layout.item_contact, parent, false);
            ViewHolder viewHolder = new ViewHolder(contactView);
            return viewHolder;
        }
    
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            Contact contact = mContacts.get(position);
    
            TextView textView = viewHolder.nameTextView;
            textView.setText(contact.getName());
    
            Button button = viewHolder.messageButton;
    
            if (contact.isOnline()) {
                button.setText("Message");
                button.setEnabled(true);
            } else {
                button.setText("Offline");
                button.setEnabled(false);
            }
        }
    
        @Override
        public int getItemCount() {
            return mContacts.size();
        }
        public static class ViewHolder extends RecyclerView.ViewHolder {
            public TextView nameTextView;
            public Button messageButton;
            public ViewHolder(View itemView) {
                super(itemView);
                nameTextView = (TextView) itemView.findViewById(R.id.contact_name);
                messageButton = (Button) itemView.findViewById(R.id.message_button);
            }
        }
    }
    

    Contact.java

    import java.util.ArrayList;
    import java.util.List;
    
    public class Contact {
        private String mName;
        private boolean mOnline;
    
        public Contact(String name, boolean online) {
            mName = name;
            mOnline = online;
        }
    
        public String getName() {
            return mName;
        }
    
        public boolean isOnline() {
            return mOnline;
        }
    
        private static int lastContactId = 0;
    
        public static List<Contact> createContactsList(int numContacts) {
            List<Contact> contacts = new ArrayList<Contact>();
    
            for (int i = 1; i <= numContacts; i++) {
                contacts.add(new Contact("Person " + ++lastContactId, i <= numContacts / 2));
            }
    
            return contacts;
        }
    }
    

    MainActivity.java

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        RecyclerView rvContacts = (RecyclerView) rootView.findViewById(R.id.view);
        ContactsAdapter adapter = new ContactsAdapter(Contact.createContactsList(20));
        rvContacts.setAdapter(adapter);
        rvContacts.setLayoutManager(new LinearLayoutManager(getContext()));
    

    please help me

  • Admin
    Admin over 5 years
    My problem was that i forgot to implement getItemCount() in the adapter. Resulted in the same behaviour, nothing showed up.
  • iamkdblue
    iamkdblue almost 5 years
    thanks, I have written wrap content NestedScrollView, now i have given 500dp to recyclerview its showing now
  • Larissa Ford
    Larissa Ford over 3 years
    Thank you so much for this answer, this saved a lot of time and stress!
  • Laila Campos
    Laila Campos over 2 years
    I forgot to implement getItemCount() as well. Thank you for this answer and for the comments. It helped a lot.