android - Cannot assign value to specific textView in RecyclerView.Adapter -
i'm new android dev , wanted make app fragments , use recyclerview inside it. i'm getting error saying i'm trying use settext on null object. code looks :
public static class contentadapter extends recyclerview.adapter<contentadapter.viewholder> { private static string[] newstitle = new string[] {...}; private static string[] newsdescs = new string[] {...}; public contentadapter(viewgroup parent) {} @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } public static class viewholder extends recyclerview.viewholder { textview titles; textview descs; public viewholder(layoutinflater inflater, viewgroup parent) { super(inflater.inflate(r.layout.item_list, parent, false)); titles = (textview) parent.findviewbyid(r.id.list_title); descs = (textview) parent.findviewbyid(r.id.list_desc); } } @override public viewholder oncreateviewholder(viewgroup parent, int viewtype) { return new viewholder(layoutinflater.from(parent.getcontext()), parent); } @override public void onbindviewholder(viewholder holder, int position) { holder.titles.settext(newstitle[position]); //here error holder.descs.settext(newsdescs[position]); } @override public int getitemcount() { return newstitle.length; } }
here correct example if view holder
public static class headervh extends recyclerview.viewholder { public textview textview; public headervh(view view){ super(view); textview = (textview)view; } }
you need pass in view object of row viewholder, not parent view object. example of oncreateviewholder()
@override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item_setting_header, parent, false); recyclerview.viewholder vh = new headervh(v); return vh; }
https://developer.android.com/reference/android/support/v7/widget/recyclerview.viewholder.html
https://www.bignerdranch.com/blog/recyclerview-part-1-fundamentals-for-listview-experts/
Comments
Post a Comment