3

I'm trying to list the data by using the new Recycler View. I've followed this tutorial & now facing issue at at one point.

Here's my

MainActivity.java

package com.example.i.newworkspace;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

TextView tv;
RecyclerView recyclerView;
RecyclerView.Adapter mAdapter;
String[] myDataset = {"One","Two","Three","Four","Five"};

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

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    mAdapter = new MyAdapter(myDataset);
    recyclerView.setAdapter(mAdapter);

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}
 class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>

{
    String[] mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView mTextView;
        public ViewHolder(TextView v) {
            super(v);
            mTextView = v;
        }
    }

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

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        ViewHolder vh = new ViewHolder((TextView) v);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.mTextView.setText(mDataset[position]);

    }

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

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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView

        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

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">

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

</LinearLayout>

Logcat:

 java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
                at com.example.i.newworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:78)
                at com.example.i.nuewworkspace.MyAdapter.onCreateViewHolder(MainActivity.java:59)

Line 78 :

        ViewHolder vh = new ViewHolder((TextView) v);
Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
user3289108
  • 770
  • 5
  • 10
  • 29

3 Answers3

8

You should notice that your my_text_view.xml is a LinearLayout so:

public static class ViewHolder extends RecyclerView.ViewHolder {

    public TextView mTextView;
    public ViewHolder(View v) {
        super(v);
        // Find the TextView in the LinearLayout
        mTextView = v.findViewById(R.id.textView));
    }
}

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

    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.my_text_view, parent, false);
    // Give the view as it is
    ViewHolder vh = new ViewHolder(v);
    return vh;
}
Gorcyn
  • 2,807
  • 1
  • 20
  • 22
  • Hey @Gorcyn, it displays the data but not in a list view like old format. I mean no divider and all. How do i do that? – user3289108 Feb 09 '15 at 11:14
  • @user3289108 An answer to that question that should help: http://stackoverflow.com/questions/24618829/how-to-add-dividers-and-spaces-between-items-in-recyclerview ;) – Gorcyn Feb 09 '15 at 13:30
2

The inflated view contains the root element too ,so u need to create your textview by findviewbyid function and then pass it to ViewHolder constructor

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

        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, null, false);
        TextView tv=(TextView)v.findViewById(R.id.textView);
        ViewHolder vh = new ViewHolder(tv);
        return vh;
    }

Good Luck

Jitty Aandyan
  • 1,994
  • 1
  • 13
  • 12
1

You are trying to cast a LinearLayout to TextView.

Change this line:

ViewHolder vh = new ViewHolder((TextView) v);

to:

ViewHolder vh = new ViewHolder((TextView) v.findViewById(R.id.textView));

Rami
  • 7,879
  • 12
  • 36
  • 66
  • The ViewHolder is here to keep references to relevant views, so we should always give him a "root" view to let it find what it needs – Gorcyn Feb 09 '15 at 10:46
  • Thanks, i didn't triy..i know this will run. But, i prefer sending root view as paramater. Added +1 for the help :) – user3289108 Feb 09 '15 at 11:11
  • @Gorcyn you are right! i forgot about the root view --- user3289108 thanks :) – Rami Feb 09 '15 at 11:26