Shammir
Shammir

Reputation: 989

RecyclerView does not display items

I am using RecyclerView in Android with a model class that has title and details. Nothing is showing. Even after log inside Adapter nothing is shown in Android Monitor.

MainActivity.class

public ArrayList<Menu> menu_list;

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

        initUi();
    }

    public void initUi(){
        RecyclerView menu_recycler = (RecyclerView) findViewById(R.id.main_menu_recycler_view);

        //set the layout manager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation( linearLayoutManager.VERTICAL );
        menu_recycler.setLayoutManager( linearLayoutManager );

        //set adapter
        MainMenuAdapter mainMenuAdapter = new MainMenuAdapter( getMenulist() );
        menu_recycler.setAdapter( mainMenuAdapter );
    }

    public ArrayList<Menu> getMenulist() {

        menu_list = new ArrayList<Menu>();

        menu_list.add( new Menu("About Us", " Learn more about our values, mission and vision ") );
        menu_list.add( new Menu("Services ", " In implementing its mandate, the Agency will provide the following to its external and internal customers:\n"));
        menu_list.add( new Menu("Stakeholders", " Who are the key players, Find out more") );
        menu_list.add( new Menu("Learn about Counterfeits", "Gain more insight on counterfeit products"));
        return menu_list;
    }

and here is my adapter

public class MainMenuAdapter extends RecyclerView.Adapter<MainMenuAdapter.MyViewHolder> {

    public List<Menu> list_of_menus;

    public MainMenuAdapter( List<Menu> list_of_menus){
        this.list_of_menus = list_of_menus;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row,parent, false);
        return new MyViewHolder(v);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        Menu menu = list_of_menus.get(position);
        Log.e("MENU IN ADAPTER TITLE", menu.title);
        holder.title_text.setText( menu.title );
        holder.details_text.setText( menu.details );

    }


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

    /*
      View Holder class
    * */

    public class MyViewHolder extends RecyclerView.ViewHolder{
        public TextView title_text, details_text;

        public MyViewHolder(View itemView) {
            super(itemView);
            title_text = (TextView) itemView.findViewById(R.id.menu_title);
            details_text = (TextView) itemView.findViewById(R.id.menu_details);
        }
    }   

}

And the Model Class

public class Menu {
    public String title, details;

    public Menu(String title, String details){
        this.details = details;
        this.title = title;
    }

}

And the layout file main_activity

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.maos.aca.MainMenuActivity">

    <android.support.v7.widget.RecyclerView
            android:layout_width="334dp"
            android:layout_height="453dp" app:layout_constraintTop_toTopOf="parent"
            android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="8dp" android:layout_marginLeft="8dp"
            app:layout_constraintLeft_toLeftOf="parent" android:layout_marginRight="8dp"
            app:layout_constraintRight_toRightOf="parent" android:id="@+id/main_menu_recycler_view"
            android:scrollbars="vertical"/>
</android.support.constraint.ConstraintLayout>

And finally the row

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical" android:layout_width="wrap_content"
              android:layout_height="wrap_content">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/menu_title"
            android:textStyle="bold"
    />
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:id="@+id/menu_details"/>
</LinearLayout>

I know this question has been asked before, but none of the answers helped me out.I'll appreciate any lead.

Upvotes: 0

Views: 1037

Answers (2)

Vamshi Krishna
Vamshi Krishna

Reputation: 46

Change this:

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

To this:

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

Upvotes: 1

Sachin Aggarwal
Sachin Aggarwal

Reputation: 1135

You need to change the getItemCount() method to this:

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

The RecyclerView will not display any elements if you are reporting that its size is zero.

Upvotes: 2

Related Questions