-1

I have been following this guide on making a RecyclerView, but I've run into a snag. The code it gives isn't very clear, but when building the custom Recyclerview.Adpater, it has this method:

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // set the view's size, margins, paddings and layout parameters
    ...
    ViewHolder vh = new ViewHolder(v);
    return vh;
}

Now, I can't seem to figure out where the R.layout.my_text_view came from. They didn't specify it in their layout file. So, when I tried to put that code into my Adapter, it told me that the symbol was not found. So, I added it as the id to my TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_recycle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".recycle.RecycleActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".recycle.RecycleActivity"/>

    <include
        layout="@layout/toolbar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

But I still can't use my_text_view in R.layout.my_text_view because it still can't find it. I also tried R.id.my_text_view, which it can find, but it says that it needs a layout, not an id. so I tried using R.layout.activity_recycle like this:

@Override
public RecycleViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    // create a new view
    TextView v = (TextView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.activity_recycle, parent, false);
    // set the view's size, margins, paddings and layout parameters

    return new ViewHolder(v);
}

But that didn't work, and I get the exception:

java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView

What am I missing? Where did this mysterious my_text_view come from, and how do I use it?

Cache Staheli
  • 3,510
  • 7
  • 32
  • 51

2 Answers2

2

Basically when using RecyclerView.Adapter's onCreateViewHolder, it returns the viewholder to the view of the list item. ViewHolder is used to inflate a single List Item (or Row) in a RecyclerView.

You should create a layout for a List Item which you want to add to RecyclerView. For example R.layout.my_text_view should contain only this TextView :

<TextView
    android:id="@+id/my_text_view_detail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

Now you can use rest of the code same as above.

Vaibhav Jain
  • 163
  • 13
0

Mistake you are doing is you are trying to inflate TextView which is inside RelativeLayout. Instead first inflate the complete layout and try to find out sub-views available inside layout.

Say for example, your layout is item_recycle_layout.xml, inflate it first and cast it into View or directly into RelativeLayout as parent layout inside your layout is RelativeLayout with id activity_recycle.

For example, your item layout is:

<RelativeLayout
  .....
  ..... >


    <TextView
        android:id="@+id/my_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

Now below code should be in your adapter:

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
    // create a new view
    RelativeLayout v = (RelativeLayout) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_recycle_layout, parent, false);
    TextView txtView = (TextView) v.findViewById(R.id.my_text_view);

    ....
    ....

    ViewHolder vh = new ViewHolder(v);
    return vh;
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295