iamcoder
iamcoder

Reputation: 539

Error in RecyclerView Implementation in Android

Can someone explain me how RecyclerView works in android?

I am using this site for reference : https://developer.android.com/training/material/lists-cards.html#CardView

But when i compile the same code i am getting an error.

Any help will be appreciated. Thanks.

MyAdapter.java

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @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;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

RecyclerActivity.java

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;

public class RecyclerActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private String[] myDataset = {"hellow"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler);
        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // specify an adapter (see also next example)
        mAdapter = new MyAdapter(myDataset);
        mRecyclerView.setAdapter(mAdapter);
    }
}

my_text_view.xml

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

</LinearLayout>

activity_recycler.xml

<?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_recycler"
    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="com.bloxofcode.listviewcustom.RecyclerActivity">

    <!-- A RecyclerView with some commonly used attributes -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

Error Encountered:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.bloxofcode.listviewcustom, PID: 27529
                  java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                      at com.bloxofcode.listviewcustom.MyAdapter.onCreateViewHolder(MyAdapter.java:37)
                      at com.bloxofcode.listviewcustom.MyAdapter.onCreateViewHolder(MyAdapter.java:12)

Upvotes: 0

Views: 478

Answers (2)

yogi
yogi

Reputation: 293

RecyclerView is more advanced version of ListView with improved performance 

and other benefits.So first of all you have to create a adapter class in which onCreateViewHolder() inflates the layout (your my_text_view.xml) and onBindViewHolder() sets the appropriate data for each row.

so first of all make some changes in your my_text_view.xml file and in adapter class.

1. add a TextView field in my_text_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

2. Do following changes in MyAdapter.java

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;
    private static final String TAG = "MyAdapter";

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mTextView;

        // change 1: ViewHolder wants View
        public ViewHolder(View view) {
            super(view);

            // change 2:  instanciate the mTextView
         /*   mTextView = v;  delete this line  */
            mTextView = (TextView) view.findViewById(R.id.tv);
        }
    }

    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {

        //Change 3: inflate the layout
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_text_view, parent, false);
        // return the view
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.mTextView.setText(mDataset[position]);
        Log.e(TAG, "data: " + mDataset[position]);

    }

    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}


3. there is no need to change MyAdapter.java n its xml file.

Upvotes: 0

Ashu
Ashu

Reputation: 3372

First of all change your my_text_view.xml like this:

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

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

</LinearLayout>

Now change your MyAdapter.java class like this:

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
       public TextView mTextView;
       public ViewHolder(View v) {
           super(v);
           mTextView = (TextView) v.findViewById(R.id.text_view);
       }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
       mDataset = myDataset;
    }

    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v      =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;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
       // - get element from your dataset at this position
       // - replace the contents of the view with that element
        holder.mTextView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
       return mDataset.length;
    }
}

Upvotes: 1

Related Questions