android - Following Google's tutorial, getting errors about ViewHolder not being able to hold a View, wants a TextView instead -


so, official documentation, bet i'm doing wrong somewhere can't understand how supposed work.

take @ example code of "creating lists , cards": https://developer.android.com/training/material/lists-cards.html

in source code of myadapter class, following lines can found:

// provide reference views each data item // complex data items may need more 1 view per item, , // provide access views data item in view holder public static class viewholder extends recyclerview.viewholder {     // each data item string in case     public textview mtextview;     public viewholder(textview v) {         super(v);         mtextview = v;     } }  // create new views (invoked layout manager) @override public myadapter.viewholder oncreateviewholder(viewgroup parent,                                                int viewtype) {     // create new view     view v = layoutinflater.from(parent.getcontext())                            .inflate(r.layout.my_text_view, parent, false);     // set view's size, margins, paddings , layout parameters     ...     viewholder vh = new viewholder(v);     return vh; } 

as can see in viewholder class, viewholder constructer defined taking textview argument. towards end of code they're creating viewholder holding v v view... how supposed work? following error

android.widget.viewholder(textview) in viewholder cannot applied android.view.view 

casting view textview causes app crash.

what missing here? pretty new android figure it's basic, apologize if it's stupid question can't wrap head around it.

consider

textview v = (textview) layoutinflater.from(parent.getcontext())                            .inflate(r.layout.my_text_view, parent, false); 

the error you're getting above because you're not casting view textview.

also not my_text_view - needs textview root element.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -