android - Access SQLite from BaseAdapter -


i have base adapter class , want acces sqlite table cursor, have trouble context. getting context right way?

public class getallentryslistviewadapter extends baseadapter {  private jsonarray dataarray; private activity activity; private static layoutinflater inflater = null; public cursor cursor; private sqlitedatabase dbase; dbhelper dbh; private context context; string pos;  public getallentryslistviewadapter(jsonarray jsonarray, activity a) {     this.dataarray = jsonarray;     this.activity = a;     inflater = (layoutinflater)this.activity.getsystemservice(context.layout_inflater_service); }  public getallentryslistviewadapter(context context) {     this.context = context; } //... dbh = new dbhelper(context);         cursor = getlikes(dbh);         cursor.movetofirst();         if (cursor.movetofirst()) {             {                 if (integer.parseint(cursor.getstring(2)) == 1) {                     cell.likeimage.setimageresource(r.drawable.heart_filled);                 }             }             while(cursor.movetonext());         }         else {             cursor.close();         }         cursor.close(); } public cursor getlikes(dbhelper dbh) {     dbase = dbh.getreadabledatabase();     string columns[] = {dbh.likes_markerid, dbh.likes_position, dbh.likes_like};     string selection = dbh.likes_markerid + " ? , " + dbh.likes_position + " ? ";     string args[] = {pinboard.markerid.tostring(), pos};     cursor cursor = dbase.query(dbh.table_likes, columns, selection, args , null, null, null, null);     return cursor; } 

this error message get:

java.lang.nullpointerexception: attempt invoke virtual method 'android.database.sqlite.sqlitedatabase android.content.context.openorcreatedatabase(java.lang.string, int, android.database.sqlite.sqlitedatabase$cursorfactory, android.database.databaseerrorhandler)' on null object reference 

the exception got because trying execute method on null object - obvious ex text.

why?
have 2 constructors - easy, thats not problem. 1 of them saving context context variable causing error occur (variable).
now, in second constructor not setting context variable keeps beeing null , trying use here:

bh = new dbhelper(context); 

and boom null pointer exception !
think thats clear : )

so change code to:

public getallentryslistviewadapter(jsonarray jsonarray, context context) {     this.dataarray = jsonarray;     this.context= context;     inflater = (layoutinflater)context.getsystemservice(context.layout_inflater_service); } 

(i bet wont need activity, not needed 99,99% of time)


Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -