android - Issue with ViewHolder pattern -
so i'm using known viewholder pattern in app. viewholder:
protected static class viewholder{ final imageview itemimage; final textview txtname; final textview txttitle; final webview webview; public viewholder (final imageview itemimage, final textview txtname, final textview txttitle, final webview webview){ this.itemimage = itemimage; this.txtname = txtname; this.txttitle = txttitle; this.webview = webview; } public viewholder (view convertview){ itemimage = (imageview) convertview.findviewbyid(r.id.fav_item_icon); txtname = (textview) convertview.findviewbyid(r.id.fav_item_name); txttitle = (textview) convertview.findviewbyid(r.id.fav_item_title); webview = (webview) convertview.findviewbyid(r.id.fav_webview); } }
so works fine if use 1st constructor like:
... public view getview(final int position, view convertview, final viewgroup parent) { final searchable searchresultsitem = searchresults.get(position); viewholder viewholder = null; if (convertview==null){ convertview = inflater.inflate(r.layout.search_list_item, null); final imageview itemimage = (imageview) convertview.findviewbyid(r.id.search_item_icon); final textview txtname = (textview) convertview.findviewbyid(r.id.search_item_name); final textview txttitle = (textview) convertview.findviewbyid(r.id.search_item_title); final webview webview = (webview) convertview.findviewbyid(r.id.search_webview); viewholder = new viewholder(itemimage, txtname, txttitle, webview); convertview.settag(viewholder); }else viewholder = (viewholder)convertview.gettag(); viewholder.webview.setfocusable(false); ...
but fails if try use 2nd constructor:
... public view getview(final int position, view convertview, final viewgroup parent) { final searchable searchresultsitem = searchresults.get(position); viewholder viewholder = null; if (convertview==null){ convertview = inflater.inflate(r.layout.search_list_item, null); viewholder = new viewholder(convertview); convertview.settag(viewholder); }else viewholder = (viewholder)convertview.gettag(); viewholder.webview.setfocusable(false); ...
it crashes npe @ line viewholder.webview.setfocusable(false)
. when try debug says created viewholder instance has fields null. why happen?
the ids seems different, intentional?
final webview webview = (webview)convertview.findviewbyid(r.id.search_webview); r.id.search_webview
(this 1 works)
and
webview = (webview) convertview.findviewbyid(r.id.fav_webview);
(this 1 not work)
r.id.fav_webview
Comments
Post a Comment