java - Custom SimpleCursorAdapter - overloaded bindView issue with onClickListener -


i have created custom simplecursoradapter in have overridden bindview can connect onclick listener of imagebutton in list item layout. want start new application on when button clicked using intent data set underlying cursor.

the problem when onclick function of button gets called, cursor seems no longer pointing @ correct row in database (i assume because has been changed point @ different row list scrolls).

here code:

private class wavefxcursoradapter extends simplecursoradapter {  public wavefxcursoradapter(context context, int layout, cursor c,     string[] from, int[] to, int flags) {     super(context, layout, c, from, to, flags); }  @override public void bindview(view v, context context, cursor c) {     super.bindview(v, context, c);     imagebutton b = (imagebutton) v.findviewbyid(r.id.btn_show_spec);      // fchr correct here:     int fchr = c.getint(c.getcolumnindex(                  wavedatacontentprovider.siteforecast.forecast_period));      log.d(tag, "chrisb: bindview: fchr is: " + fchr );      b.setonclicklistener(new onclicklistener() {         @override         public void onclick(view v) {             intent = new intent(getactivity(), specdrawactivity.class);             i.setaction(intent.action_view);             i.putextra("com.kernowsoft.specdraw.sitename", msitename);              // fchr not correct here! can't use fchr             // bindview method lint tells me error:             int fchr = c.getint(c.getcolumnindex(                  wavedatacontentprovider.siteforecast.forecast_period));              log.d(tag, "bindview: forecast hour is: " + fchr);             i.putextra("com.kernowsoft.specdraw.fchr", fchr);             getactivity().startactivity(i);         }     }); } 

as can see comments in code above, fchr correct when print log in bindview incorrect in onclick method. tried referencing fchr variable in bindview onclick method, andriod lint tells me cant this:

cannot refer non-final variable fchr inside inner class defined in different method

my question is: how can correctly pass fchr variable cursor onclick method?

thanks!

the reason error variable fchr local variable in bindview() method. object create anonymous class might last until after bindview() method returns.

when bindview() method returns, local variables cleaned stack, won't exist anymore after bindview() returns.

but anonymous class object reference variable fchr. things go horribly wrong if anonymous class object tries access variable after have been cleaned up.

by making fchr final, not variables anymore, constants. compiler can replace use of fchr in anonymous class values of constants, , won't have problem accessing non-existent variables anymore.

see working inner classes


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -