?
Solved

How to get Application context inside a class that doesnot extends Activity in android ?

Posted on 2011-09-29
2
Medium Priority
?
1,515 Views
Last Modified: 2013-11-24
I had a  Database class which contains all the database related functions.I want to call that class in another class .Is it possible?

i want to use a insert function in that class.But my problem is

 "DatabaseActivity dbase = new DatabaseActivity(this);

shows error.Does anybody help me?

My database class name is "DatabaseActivity".

How can i get the context?

In DatabaseActivity class ,there is a constructor which is created while extending Sqliteopenhelper.



------------------------------------------------------------------------------

my second class is

public class Webservice  {

      DatabaseActivity dbase  ;

public String getAllUsers(String Key, int DeviceId)

      {
        //DatabaseActivity dbase = new DatabaseActivity(this);
           

            }
}

i cant call my databaseactivity class  in the second class.It shows "constructor DatabaseActivity is undefined"

can anybody fix my issue?
0
Comment
Question by:Tore123
  • 2
2 Comments
 
LVL 1

Accepted Solution

by:
Gyufa earned 2000 total points
ID: 36898230
Hi!

I also use database in one of my applications, and my solution was to make the database manager class singleton.

public class DatabaseManager {
   
    private static DatabaseManager instance = null;

    protected DatabaseManager(final Context context) {
        // do initialization stuff
    }

    public static void init(final Context context) {
        if(instance == null) {
            instance = new ClassicSingleton();
         }
     }

    public static DatabaseManager getInstance() {
        return instance;
    }

    public Object query1(long id) {}
    ...

}

Open in new window


Now you can write a custom Application class, and initialize DatabaseManager there:

http://www.helloandroid.com/tutorials/maintaining-global-application-state

After that, you can use your database manager all over your application, it won't be context dependent.

I hope it helps,

Gyufa
0
 
LVL 1

Expert Comment

by:Gyufa
ID: 36898232
    ...
    public static void init(final Context context) {
        if(instance == null) {
            instance = new DatabaseManager(context);
         }
     }

    public static DatabaseManager getInstance(final Context context) {
        return instance;
    }
    ...

Open in new window


Sorry, i copied the code. :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Creating a Cordova application which allow user to save to/load from his Dropbox account the application database.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Simple Linear Regression
Suggested Courses
Course of the Month16 days, 12 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question