Solved

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

Posted on 2011-09-29
2
1,448 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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Contacts, help Android 5 37
Protecting a SKY 4.0 (Android) devise 15 102
Is doing tutor.com teaching in my situation advisable? 2 54
difference of if loops 23 41
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now