This is the Java file of the fragment I wish to insert recycler view. Please dont worry about the package name which I deleted for some reasons.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class Dashboard extends Fragment {
    private RecyclerView recyclerView;
    private RecyclerAdapter adapter;

    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static Dashboard newInstance() {
        Dashboard fragment = new Dashboard();
        return fragment;
    }

    public Dashboard () {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.activity_dashboard, container,
                false);
        recyclerView = (RecyclerView) rootView.findViewById(R.id.drawerlist);
        adapter=new RecyclerAdapter(getActivity().getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return rootView;
    }

    public static List<Information> getData(){
        List<Information> data=new ArrayList<>();
        String[] titles={"Rooms Occupied","RoomsVacant","Check-In","Check-Out","Extensions","Confirmations","Cancellations"};
        for(int i=0;i<titles.length;i++)
        {
            Information current =new Information();
            current.title=titles[i];
            data.add(current);
        }
        return data;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }






}

This is my Adapter class

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.TextView;

import java.util.Collections;
import java.util.List;

/**
 * Created by gowtham on 6/13/2015.
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    List<Information> data= Collections.emptyList();
    public RecyclerAdapter(Context context, List<Information> data){
        inflater=LayoutInflater.from(context);
        this.data=data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=inflater.inflate(R.layout.custom_row, parent,false);
        MyViewHolder holder=new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Information current=data.get(position);
        holder.title.setText(current.title);

    }

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

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title;


        public MyViewHolder(View itemView) {
            super(itemView);
            title= (TextView) itemView.findViewById(R.id.viewText);

        }
    }
}

This is Layout file of the fragment in which I want to use the recycler view

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/viewImage"
        android:background="@drawable/ic_dashboard"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/viewText"
        android:text="Dummy Text"
        android:textStyle="bold"
        android:layout_gravity="left"
        android:gravity="center" />
</LinearLayout>

Since I'm new to stackoverflow I don't have enough reputations to add image. So I will attach the screenshot in the comments below. Please take a look at it to know what is happening here exactly. Thanks!

1

There are 1 answers

5
Ellie Zou On BEST ANSWER

Why you call getActivity().getData(), just call getData().

And in your Adapter class:

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

It can't just return zero, you should return the data size.