Daozui
Daozui

Reputation: 79

Reason of RecyclerView Not Loading Data

I tried to create a List with RecyclerView in Android. However, I don't understand why the RecyclerView could not load data properly. I tried to compare my codes with the example available online and I could not tell what is the difference between example codes and my codes.

Can anyone help to point out what is missing or what is wrong with my code?

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="daozui.assignment3_task3.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>    
</LinearLayout>

contact_arrangement.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/contactIcon_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/man"
        android:layout_margin="10dp"/>

    <TextView
        android:id="@+id/contactName_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="30sp"
        android:text="Name"
        android:layout_toEndOf="@id/contactIcon_ID"
        android:layout_marginTop="25dp"
        android:layout_marginStart="25dp"/>

    <TextView
        android:id="@+id/contactRelationship_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFBEB9B9"
        android:textSize="20sp"
        android:text="Relationship"
        android:layout_alignStart="@id/contactName_ID"
        android:layout_below="@id/contactName_ID"/>

</RelativeLayout>

MainActivity.Java

package daozui.assignment3_task3;

import android.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    RecyclerView theRecyclerView;
    List<Contact> contactList;
    ContactAdapter theAdapter;

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

        createList();

        theRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView);
        theRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        theAdapter = new ContactAdapter(contactList);
        theRecyclerView.setAdapter(theAdapter);
    }

    private void createList() {
        contactList = new ArrayList<Contact>();
        contactList.add(new Contact("Alex", "male", "Friends", "0123456789"));
        contactList.add(new Contact("Mona", "female", "Friends", "9876543210"));
    }
}

ContactAdapter.Java

package daozui.assignment3_task3;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.theViewHolder> {
    private List<Contact> contacList;

    public ContactAdapter(List<Contact> ContactList) {
        this.contacList = ContactList;
    }

    @Override
    public ContactAdapter.theViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.contact_arrangement, parent, false);
        return new theViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(theViewHolder holder, int position) {
        Contact contact = contacList.get(position);
        holder.contactName.setText(contact.getName());
        holder.contactRelationship.setText(contact.getRelationship());

        if (contact.getGender().equals("male")) {
            holder.contactIcon.setImageResource(R.drawable.man);
        } else {
            holder.contactIcon.setImageResource(R.drawable.woman);
        }
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public static class theViewHolder extends RecyclerView.ViewHolder {

        public TextView contactName, contactRelationship;
        public ImageView contactIcon;

        public theViewHolder(View itemView) {
            super(itemView);
            contactIcon = (ImageView) itemView.findViewById(R.id.contactIcon_ID);
            contactName = (TextView) itemView.findViewById(R.id.contactName_ID);
            contactRelationship = (TextView) itemView.findViewById(R.id.contactRelationship_ID);
            //            itemView.setOnClickListener(this);

        }

        //        @Override
        //        public void onClick(View view)
        //        {
        //            int pos= getAdapterPosition();
        //            Toast.makeText(itemView.getContext(),contacList.get(pos).getContactNumber(),Toast.LENGTH_LONG);
        //        }
    }
}

Contact.Java

package daozui.assignment3_task3;

public class Contact {
    private String name;
    private String gender;
    private String relationship;
    private String contactNumber;

    public Contact(String Name, String Gender, String Relationship, String ContactNumber) {
        this.name = Name;
        this.gender = Gender;
        this.relationship = Relationship;
        this.contactNumber = ContactNumber;
    }

    public String getName() {
        return name;
    }

    public String getGender() {
        return gender;
    }

    public String getRelationship() {
        return relationship;
    }

    public String getContactNumber() {
        return contactNumber;
    }
}

Upvotes: 0

Views: 1987

Answers (4)

tommytucker7182
tommytucker7182

Reputation: 243

In the event the list is null, use this simple ternary to avoid a null pointer exception;

@Override
    public int getItemCount() {
        return contactList == null ? 0 : contactList.size();
    }

Upvotes: 0

Atif AbbAsi
Atif AbbAsi

Reputation: 6035

notify your adapter that data is updated.!

notifyDataSetChanged();



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

Upvotes: 0

Deep Lathia
Deep Lathia

Reputation: 760

You should return the size of your collection here:

@Override
public void getItemCount(){
  return contactList.size();
}

getItemCount() Returns the total number of items in the data set held by the adapter.

The int value that getItemCount() returns is the number of times the RecyclerView is going to look for data from your collection to bind.

Upvotes: 1

The problem is here:

  @Override
public int getItemCount() {
    return 0;
}

You should return the size of your list:

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

Upvotes: 5

Related Questions