Thursday, December 30, 2010

[android-developers] Re: Access database (in static method?) in non-Activity class

So I have DatabaseOptions.java class

public class DatabaseOptions extends Activity {

private static Context gContext;
private static SQLiteDatabase db;

public DatabaseOptions(Context context) {
gContext = context.getApplicationContext();
db = openOrCreateDatabase(DATABASE_NAME, gContext.MODE_PRIVATE,
null);

db.execSQL("CREATE TABLE IF NOT EXISTS " + PARTNER_TABLE_NAME +
" (id INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " VARCHAR);");
}

public ArrayList<String> getAllItems() {
ArrayList<String> items = new ArrayList<String>();

Cursor result = db.rawQuery("SELECT * FROM " + PARTNER_TABLE_NAME +
" ORDER BY name", null);
result.moveToFirst();

while (!result.isAfterLast()) {
String name = result.getString(result.getColumnIndex("name"));
items.add(name);
result.moveToNext();
}

return items;
}
}

And I have MainActivity class

public class PartnersActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.partners);

Partner newPartner = new Partner(this);
partnersItems = newPartner.getAllItems();
}
}

But I'm getting error on line "db =
openOrCreateDatabase(DATABASE_NAME, gContext.MODE_PRIVATE, null);"?
Also for every static method I should create own local "db" variable?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate