What's wrong with my implementation of RecyclerView?










0















I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.



The code taken from here : RecyclerView



This is my code, including all necessary files:



MainActivity.java:



package com.example.lastlocation.recyclerviewer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
String myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";

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




This is MyAdapter.java:



package com.example.lastlocation.recyclerviewer;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
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 MyViewHolder extends RecyclerView.ViewHolder
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(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.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)


TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;


// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder 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;




this is activity_main.xml:



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

<View
android:id="@+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view"
app:srcCompat="@android:color/holo_orange_dark" />

<TextView
android:id="@+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />


<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintVertical_bias="1.0" />



</android.support.constraint.ConstraintLayout>


And this is the xml for a single text view called my_text_view.xml:



<?xml version="1.0" encoding="utf-8"?>


<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

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


</LinearLayout>


As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.










share|improve this question

















  • 1





    In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

    – Nilesh Rathod
    Nov 16 '18 at 9:44












  • I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

    – user9644796
    Nov 16 '18 at 9:49












  • TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

    – PPartisan
    Nov 16 '18 at 9:51















0















I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.



The code taken from here : RecyclerView



This is my code, including all necessary files:



MainActivity.java:



package com.example.lastlocation.recyclerviewer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
String myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";

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




This is MyAdapter.java:



package com.example.lastlocation.recyclerviewer;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
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 MyViewHolder extends RecyclerView.ViewHolder
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(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.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)


TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;


// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder 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;




this is activity_main.xml:



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

<View
android:id="@+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view"
app:srcCompat="@android:color/holo_orange_dark" />

<TextView
android:id="@+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />


<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintVertical_bias="1.0" />



</android.support.constraint.ConstraintLayout>


And this is the xml for a single text view called my_text_view.xml:



<?xml version="1.0" encoding="utf-8"?>


<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

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


</LinearLayout>


As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.










share|improve this question

















  • 1





    In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

    – Nilesh Rathod
    Nov 16 '18 at 9:44












  • I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

    – user9644796
    Nov 16 '18 at 9:49












  • TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

    – PPartisan
    Nov 16 '18 at 9:51













0












0








0








I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.



The code taken from here : RecyclerView



This is my code, including all necessary files:



MainActivity.java:



package com.example.lastlocation.recyclerviewer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
String myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";

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




This is MyAdapter.java:



package com.example.lastlocation.recyclerviewer;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
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 MyViewHolder extends RecyclerView.ViewHolder
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(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.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)


TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;


// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder 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;




this is activity_main.xml:



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

<View
android:id="@+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view"
app:srcCompat="@android:color/holo_orange_dark" />

<TextView
android:id="@+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />


<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintVertical_bias="1.0" />



</android.support.constraint.ConstraintLayout>


And this is the xml for a single text view called my_text_view.xml:



<?xml version="1.0" encoding="utf-8"?>


<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

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


</LinearLayout>


As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.










share|improve this question














I am following the Android Developer's explanatin on how to set RecyclerView, but I get some errors, for example android.widget.LinearLayout cannot be cast to android.widget.TextView.



The code taken from here : RecyclerView



This is my code, including all necessary files:



MainActivity.java:



package com.example.lastlocation.recyclerviewer;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

public class MainActivity extends AppCompatActivity
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
String myDataset = new String[5];
myDataset[0] = "Data0";
myDataset[1] = "Data1";
myDataset[2] = "Data2";
myDataset[3] = "Data3";
myDataset[4] = "Data4";

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




This is MyAdapter.java:



package com.example.lastlocation.recyclerviewer;

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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>
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 MyViewHolder extends RecyclerView.ViewHolder
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(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.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType)


TextView v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;


// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder 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;




this is activity_main.xml:



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

<View
android:id="@+id/view"
android:layout_width="386dp"
android:layout_height="135dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<ImageView
android:id="@+id/imageView"
android:layout_width="66dp"
android:layout_height="64dp"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/view"
app:srcCompat="@android:color/holo_orange_dark" />

<TextView
android:id="@+id/textView2"
android:layout_width="68dp"
android:layout_height="30dp"
android:layout_marginTop="8dp"
android:text="John Doe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />


<!-- A RecyclerView with some commonly used attributes -->
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="375dp"
android:layout_height="367dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.428"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view"
app:layout_constraintVertical_bias="1.0" />



</android.support.constraint.ConstraintLayout>


And this is the xml for a single text view called my_text_view.xml:



<?xml version="1.0" encoding="utf-8"?>


<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

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


</LinearLayout>


As you can see I nearly copied the code from the Android Developer's website, but I still don't know where is the problem.







java android android-recyclerview






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 9:42







user9644796














  • 1





    In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

    – Nilesh Rathod
    Nov 16 '18 at 9:44












  • I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

    – user9644796
    Nov 16 '18 at 9:49












  • TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

    – PPartisan
    Nov 16 '18 at 9:51












  • 1





    In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

    – Nilesh Rathod
    Nov 16 '18 at 9:44












  • I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

    – user9644796
    Nov 16 '18 at 9:49












  • TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

    – PPartisan
    Nov 16 '18 at 9:51







1




1





In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

– Nilesh Rathod
Nov 16 '18 at 9:44






In Your single list item the hight of your LinearLayout should be wrap_content like android:layout_height="wrap_content"

– Nilesh Rathod
Nov 16 '18 at 9:44














I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

– user9644796
Nov 16 '18 at 9:49






I keep getting the same error, do you mind testing it yourself to see maybe for you it does work? For me it didn't. Thanks

– user9644796
Nov 16 '18 at 9:49














TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

– PPartisan
Nov 16 '18 at 9:51





TextView v = (TextView) LayoutInflater.from(parent.getContext()) .inflate(R.layout.my_text_view, parent, false); - it's clear from your code that the root tag for my_text_view is a LinearLayout, not TextView, so of course you will get a casting exception

– PPartisan
Nov 16 '18 at 9:51












3 Answers
3






active

oldest

votes


















0














 public static class MyViewHolder extends RecyclerView.ViewHolder 
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(TextView v)
super(v);
mTextView = v;



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



The problem is in your ViewHolder constructor it should be like that



public static class MyViewHolder extends RecyclerView.ViewHolder 
// each data item is just a string in this case
public TextView mTextView;
public MyViewHolder(View v)
super(v);
mTextView = v.findViewById(R.id.your_text_view_id);




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


View v = (TextView) LayoutInflater.from(parent.getContext())
.inflate(R.layout.my_text_view, parent, false);

MyViewHolder vh = new MyViewHolder(v);
return vh;



You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.






share|improve this answer






























    0














    You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below



    @Override
    public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
    return vh;



    and change your viewholder as below



    public static class MyViewHolder extends RecyclerView.ViewHolder 
    // each data item is just a string in this case
    public TextView mTextView;
    public MyViewHolder(View v)
    super(v);
    mTextView = v.findViewById(R.id.my_text_view);







    share|improve this answer






























      0














      Just Simply you need to change your onCreateViewHolder method by taking as



      View view = LayoutInflater.from(parent.getContext())
      .inflate(R.layout.my_text_view, parent, false);
      return view;





      share|improve this answer























      • You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

        – user9644796
        Nov 16 '18 at 10:14












      • but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

        – Shivam Gautam
        Nov 16 '18 at 12:33












      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335129%2fwhats-wrong-with-my-implementation-of-recyclerview%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown
























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














       public static class MyViewHolder extends RecyclerView.ViewHolder 
      // each data item is just a string in this case
      public TextView mTextView;
      public MyViewHolder(TextView v)
      super(v);
      mTextView = v;



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



      The problem is in your ViewHolder constructor it should be like that



      public static class MyViewHolder extends RecyclerView.ViewHolder 
      // each data item is just a string in this case
      public TextView mTextView;
      public MyViewHolder(View v)
      super(v);
      mTextView = v.findViewById(R.id.your_text_view_id);




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


      View v = (TextView) LayoutInflater.from(parent.getContext())
      .inflate(R.layout.my_text_view, parent, false);

      MyViewHolder vh = new MyViewHolder(v);
      return vh;



      You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
      The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.






      share|improve this answer



























        0














         public static class MyViewHolder extends RecyclerView.ViewHolder 
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(TextView v)
        super(v);
        mTextView = v;



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



        The problem is in your ViewHolder constructor it should be like that



        public static class MyViewHolder extends RecyclerView.ViewHolder 
        // each data item is just a string in this case
        public TextView mTextView;
        public MyViewHolder(View v)
        super(v);
        mTextView = v.findViewById(R.id.your_text_view_id);




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


        View v = (TextView) LayoutInflater.from(parent.getContext())
        .inflate(R.layout.my_text_view, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;



        You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
        The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.






        share|improve this answer

























          0












          0








          0







           public static class MyViewHolder extends RecyclerView.ViewHolder 
          // each data item is just a string in this case
          public TextView mTextView;
          public MyViewHolder(TextView v)
          super(v);
          mTextView = v;



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



          The problem is in your ViewHolder constructor it should be like that



          public static class MyViewHolder extends RecyclerView.ViewHolder 
          // each data item is just a string in this case
          public TextView mTextView;
          public MyViewHolder(View v)
          super(v);
          mTextView = v.findViewById(R.id.your_text_view_id);




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


          View v = (TextView) LayoutInflater.from(parent.getContext())
          .inflate(R.layout.my_text_view, parent, false);

          MyViewHolder vh = new MyViewHolder(v);
          return vh;



          You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
          The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.






          share|improve this answer













           public static class MyViewHolder extends RecyclerView.ViewHolder 
          // each data item is just a string in this case
          public TextView mTextView;
          public MyViewHolder(TextView v)
          super(v);
          mTextView = v;



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



          The problem is in your ViewHolder constructor it should be like that



          public static class MyViewHolder extends RecyclerView.ViewHolder 
          // each data item is just a string in this case
          public TextView mTextView;
          public MyViewHolder(View v)
          super(v);
          mTextView = v.findViewById(R.id.your_text_view_id);




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


          View v = (TextView) LayoutInflater.from(parent.getContext())
          .inflate(R.layout.my_text_view, parent, false);

          MyViewHolder vh = new MyViewHolder(v);
          return vh;



          You are trying to assign LinearLayout which is the View in your constructor to your TextView and it is crashing. This will fix the problem.
          The view you receive as a constructor param in your ViewHolder is the root view of your ViewHolder, in your case a LinearLayout, from this root view you can find the other views in it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 9:51









          T.DimitrovT.Dimitrov

          13727




          13727























              0














              You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below



              @Override
              public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
              LayoutInflater inflater = LayoutInflater.from(parent.getContext());
              ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
              return vh;



              and change your viewholder as below



              public static class MyViewHolder extends RecyclerView.ViewHolder 
              // each data item is just a string in this case
              public TextView mTextView;
              public MyViewHolder(View v)
              super(v);
              mTextView = v.findViewById(R.id.my_text_view);







              share|improve this answer



























                0














                You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below



                @Override
                public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
                return vh;



                and change your viewholder as below



                public static class MyViewHolder extends RecyclerView.ViewHolder 
                // each data item is just a string in this case
                public TextView mTextView;
                public MyViewHolder(View v)
                super(v);
                mTextView = v.findViewById(R.id.my_text_view);







                share|improve this answer

























                  0












                  0








                  0







                  You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below



                  @Override
                  public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
                  LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                  ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
                  return vh;



                  and change your viewholder as below



                  public static class MyViewHolder extends RecyclerView.ViewHolder 
                  // each data item is just a string in this case
                  public TextView mTextView;
                  public MyViewHolder(View v)
                  super(v);
                  mTextView = v.findViewById(R.id.my_text_view);







                  share|improve this answer













                  You are inflating row view wrongly and thus not assigning views in viewholder in proper manner change your code as below



                  @Override
                  public UserListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
                  LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                  ViewHolder vh = new ViewHolder((inflater.inflate(R.layout.your_row_layout, parent, false)));
                  return vh;



                  and change your viewholder as below



                  public static class MyViewHolder extends RecyclerView.ViewHolder 
                  // each data item is just a string in this case
                  public TextView mTextView;
                  public MyViewHolder(View v)
                  super(v);
                  mTextView = v.findViewById(R.id.my_text_view);








                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 9:51









                  Karan MerKaran Mer

                  5,67632966




                  5,67632966





















                      0














                      Just Simply you need to change your onCreateViewHolder method by taking as



                      View view = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.my_text_view, parent, false);
                      return view;





                      share|improve this answer























                      • You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                        – user9644796
                        Nov 16 '18 at 10:14












                      • but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                        – Shivam Gautam
                        Nov 16 '18 at 12:33
















                      0














                      Just Simply you need to change your onCreateViewHolder method by taking as



                      View view = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.my_text_view, parent, false);
                      return view;





                      share|improve this answer























                      • You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                        – user9644796
                        Nov 16 '18 at 10:14












                      • but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                        – Shivam Gautam
                        Nov 16 '18 at 12:33














                      0












                      0








                      0







                      Just Simply you need to change your onCreateViewHolder method by taking as



                      View view = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.my_text_view, parent, false);
                      return view;





                      share|improve this answer













                      Just Simply you need to change your onCreateViewHolder method by taking as



                      View view = LayoutInflater.from(parent.getContext())
                      .inflate(R.layout.my_text_view, parent, false);
                      return view;






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 10:12









                      Shivam GautamShivam Gautam

                      112




                      112












                      • You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                        – user9644796
                        Nov 16 '18 at 10:14












                      • but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                        – Shivam Gautam
                        Nov 16 '18 at 12:33


















                      • You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                        – user9644796
                        Nov 16 '18 at 10:14












                      • but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                        – Shivam Gautam
                        Nov 16 '18 at 12:33

















                      You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                      – user9644796
                      Nov 16 '18 at 10:14






                      You only removed the (TextView)? Btw the other answer worked (To only have the TextView in the xml file

                      – user9644796
                      Nov 16 '18 at 10:14














                      but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                      – Shivam Gautam
                      Nov 16 '18 at 12:33






                      but you are using only one textview in your custom layout,when you use more than one textview or button then it shows error ,So you need to put your textview in one layout(linear,relative or constraint layout) and inflate your layout in View class in your method.

                      – Shivam Gautam
                      Nov 16 '18 at 12:33


















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335129%2fwhats-wrong-with-my-implementation-of-recyclerview%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線