java - RecyclerView in Fragment -
i setting recyclerview
inside fragment
following android docs example here.
i pasted code below, there not much. (the difference i'm using fragment instead of activity).
the error i'm getting is:
java.lang.illegalstateexception: specified child has parent. must call removeview() on child's parent first.
all solutions problem online add false
argument in layout inflation fragment. i've done this, , still error.
my code:
mainactivity
... mfragmentmanager = getsupportfragmentmanager(); mfragmenttransaction = mfragmentmanager.begintransaction(); mfragmenttransaction.replace(r.id.containerview,new newsfragment()).commit(); ...
newsfragment
public class newsfragment extends fragment { private recyclerview mrecyclerview; private recyclerview.adapter madapter; private recyclerview.layoutmanager mlayoutmanager; @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.news_layout,container, false); mrecyclerview = (recyclerview) v.findviewbyid(r.id.my_recycler_view); mrecyclerview.sethasfixedsize(true); mlayoutmanager = new linearlayoutmanager(getactivity()); mrecyclerview.setlayoutmanager(mlayoutmanager); madapter = new newsadapter(new string[]{"hello","world","qwert","test","greg","peck"}); mrecyclerview.setadapter(madapter); return v; } }
newsadapter
public class newsadapter extends recyclerview.adapter<newsadapter.viewholder> { private string[] mdataset; public static class viewholder extends recyclerview.viewholder { public textview mtextview; public viewholder(textview tv) { super(tv); mtextview = tv; } } public newsadapter(string[] mydataset) { mdataset = mydataset; } @override public newsadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.news_item, parent, false); textview tv = (textview)v.findviewbyid(r.id.textview2); viewholder vh = new viewholder(tv); return vh; } @override public void onbindviewholder(viewholder holder, int position) { holder.mtextview.settext(mdataset[position]); } @override public int getitemcount() { return mdataset.length; } }
any hugely appreciated!
the error on line:
textview tv = (textview)v.findviewbyid(r.id.textview2); viewholder vh = new viewholder(tv);
change to:
textview tv = (textview)v.findviewbyid(r.id.textview2); viewholder vh = new viewholder(v);
the text view having parent. that's why getting error.
updated code:
public class newsadapter extends recyclerview.adapter<newsadapter.viewholder> { private string[] mdataset; public static class viewholder extends recyclerview.viewholder { public textview mtextview; public viewholder(view view) { super(tv); mtextview= (textview)view.findviewbyid(r.id.textview2); } } public newsadapter(string[] mydataset) { mdataset = mydataset; } @override public newsadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) { view v = layoutinflater.from(parent.getcontext()) .inflate(r.layout.news_item, parent, false); viewholder vh = new viewholder(v); return vh; } @override public void onbindviewholder(viewholder holder, int position) { holder.mtextview.settext(mdataset[position]); } @override public int getitemcount() { return mdataset.length; } }
Comments
Post a Comment