android - How to show multiple viewholders in recycler view? -
i tried understand how can tell 2 different views inflated depending on information on object...
my setup keep crashing error:
java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.textview.settypeface(android.graphics.typeface)' on null object reference
which points line:
myviewholder.commentusername.settypeface(boldtypeface);
this adapter:
public class commentsadapter extends recyclerview.adapter<recyclerview.viewholder> { private list<databasecomment> dbcommentslist; private databasehelper db; private context context; private typeface typeface, italictypeface, boldtypeface; public class myviewholder extends recyclerview.viewholder implements view.onclicklistener{ public textview commentusername, commentusermsg, commentuserdate, commentuserremove; public imageview emojiicon; public myviewholder(view view) { super(view); commentusername = (textview) view.findviewbyid(r.id.useradapterusername); commentusermsg = (textview) view.findviewbyid(r.id.commentusermsg); commentuserdate = (textview) view.findviewbyid(r.id.commentuserdate); commentuserremove = (textview) view.findviewbyid(r.id.commentuserremove); emojiicon = (imageview) view.findviewbyid(r.id.emojiicon); log.d(constants.debug, "in view holder"); view.setonclicklistener(this); commentuserremove.setonclicklistener(this); } @override public void onclick(view v) { if (monentryclicklistener != null) { log.d(constants.debug, "in on click"); monentryclicklistener.onentryclick(v, getadapterposition()); } } } private static onentryclicklistener monentryclicklistener; public interface onentryclicklistener { void onentryclick(view view, int position); } public void setonentryclicklistener(onentryclicklistener onentryclicklistener) { monentryclicklistener = onentryclicklistener; } public class myfeatureviewholder extends recyclerview.viewholder { public textview commentcompany, commentcompanymsg, commentcompanydate; public imageview emojiicon; public myfeatureviewholder(view view) { super(view); commentcompany = (textview) view.findviewbyid(r.id.commentcompany); commentcompanymsg = (textview) view.findviewbyid(r.id.commentcompanymsg); commentcompanydate = (textview) view.findviewbyid(r.id.commentcompanydate); emojiicon = (imageview) view.findviewbyid(r.id.emojiicon); log.d(constants.debug, "in view holder"); } } public commentsadapter(context mcontext, list<databasecomment> comments, typeface mytypeface, typeface mytypefaceitalic, typeface mytypefacebold) { context = mcontext; db = new databasehelper(context); dbcommentslist = comments; typeface = mytypeface; italictypeface = mytypefaceitalic; boldtypeface = mytypefacebold; } @override public recyclerview.viewholder oncreateviewholder(viewgroup parent, int viewtype) { switch (viewtype){ case 0: return new myfeatureviewholder(layoutinflater.from(parent.getcontext()) .inflate(r.layout.comment_business_item, parent, false)); case 1: return new myfeatureviewholder(layoutinflater.from(parent.getcontext()) .inflate(r.layout.comment_user_item, parent, false)); } return new myviewholder(layoutinflater.from(parent.getcontext()) .inflate(r.layout.comment_user_item, parent, false)); } @override public void onbindviewholder(recyclerview.viewholder holder, int position) { //int pos = getitemviewtype(position); //is business comment if(dbcommentslist.get(position).getistype() == 0) { myfeatureviewholder featureviewholder = (myfeatureviewholder) holder; databasecomment dbcomment = dbcommentslist.get(position); featureviewholder.commentcompany.settypeface(boldtypeface); featureviewholder.commentcompanymsg.settypeface(typeface); featureviewholder.commentcompanydate.settypeface(italictypeface); featureviewholder.commentcompany.settext(dbcomment.getusername()); featureviewholder.commentcompanymsg.settext(dbcomment.getcommenttext()); calendar date = calendar.getinstance(); date.settimeinmillis(dbcomment.getcommentdate()); string commentdatetxt = (date.get(calendar.month) + "." + date.get(calendar.day_of_month) + "." + date.get(calendar.year)); featureviewholder.commentcompanydate.settext(commentdatetxt); //anything greater 0 user comment } else { //todo show x button near viewholder if ischanged 1 myviewholder myviewholder = (myviewholder) holder; if(dbcommentslist.get(position).getischanged() == 1) { myviewholder.commentuserremove.setvisibility(view.visible); } else { myviewholder.commentuserremove.setvisibility(view.gone); } databasecomment dbcomment = dbcommentslist.get(position); myviewholder.commentusername.settypeface(boldtypeface); myviewholder.commentusermsg.settypeface(typeface); myviewholder.commentuserdate.settypeface(italictypeface); myviewholder.commentusername.settext(dbcomment.getusername()); myviewholder.commentusermsg.settext(dbcomment.getcommenttext()); calendar date = calendar.getinstance(); date.settimeinmillis(dbcomment.getcommentdate()); string commentdatetxt = (date.get(calendar.month) + "." + date.get(calendar.day_of_month) + "." + date.get(calendar.year)); myviewholder.commentuserdate.settext(commentdatetxt); int[] commentsimagelist = new int[]{r.drawable.ic_announcement_black_18dp, r.drawable.ic_announcement_black_18dp, r.drawable.ic_announcement_black_18dp, r.drawable.ic_explore_black_18dp}; myviewholder.emojiicon.setimageresource(commentsimagelist[dbcomment.getistype()]); } //grab more comments if(position > (dbcommentslist.size() - 3) && (dbcommentslist.size() % 20) == 0) { grabmorecomments(); } } private void grabmorecomments() { //todo //grabapi - offset dbcommentslist.size - in limit of 20 } @override public int getitemcount() { return dbcommentslist.size(); } @override public int getitemviewtype(int position) { if(dbcommentslist.get(position).getistype() == 0) { return 0; } return 1; } }
this class setup adapter:
private void setupadapter() { commentsadapter = new commentsadapter(this, dbcommentslist, typefaceprovider.gettypeface(this, 0), typefaceprovider.gettypeface(this, 1), typefaceprovider.gettypeface(this, 2)); recyclerview.layoutmanager mlayoutmanager = new linearlayoutmanager(getapplicationcontext()); commentsrecyclerview.setlayoutmanager(mlayoutmanager); commentsrecyclerview.setitemanimator(new defaultitemanimator()); //todo check clicking on comment business nothing happens commentsadapter.setonentryclicklistener(new commentsadapter.onentryclicklistener() { @override public void onentryclick(view view, int position) { databasecomment comment = dbcommentslist.get(position); textview deletebtn = (textview) view.findviewbyid(r.id.commentuserremove); if(view == deletebtn) { //used remove comment db , list db.removesinglecomment(comment); dbcommentslist.remove(position); commentsadapter.notifydatasetchanged(); } else { toast.maketext(getapplicationcontext(), comment.getusername() + " selected!", toast.length_short).show(); taketouserprofile(dbcommentslist.get(position)); } } }); commentsrecyclerview.setadapter(commentsadapter); commentsadapter.notifydatasetchanged(); }
so in adapter getitemviewtype not being done correctly... how if comments istype 0 show 1 view , else show other view?
based on error looks view id r.id.useradapterusername missing in layout file comment_user_item.xml
Comments
Post a Comment