"gContext." - ofcourse, I was calling it on Partner activity (context)
not the one I passed into it (actually applicationContext). Now I get
it ;)

I understand that reference to the context that I pass in will go away
if activity is destroyed (and because of that I use application
context which will stay until whole application is destroyed), but
when I should use it (activity context) any why? Some example would
help me. Some explanation of the "barrier"/boundary between those two.

On Dec 30, 2:25 pm, Kostya Vasilyev <[email protected]> wrote:
> As far as I can see, your DatabaseOptions is not a real Activity, but
> rather your helper class. If so, don't derive it from Activity.
>
> The line that's failing to compile should be:
>
> db = gContext.openOrCreateDatabase(DATABASE_NAME, Context.MODE_PRIVATE, null);
>
> You have gContext, which you can keep for the duration of your
> application, that was the point, keep it and use to call method of Context.
>
> I'm not sure how Partner relates to DatabaseOptions.
>
> If you're going to instantiate a new DatabaseOptions (or one of its
> subclasses) every time you need to access data, then add a check to see
> if the database has already been opened (db != null), and if so, reuse it.
>
> -- Kostya
>
> 30.12.2010 15:11, svebee пишет:
>
>
>
> > 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?
>
> --
> Kostya Vasilyev -- WiFi Manager + pretty widget 
> --http://kmansoft.wordpress.com

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

Reply via email to