?
Solved

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

Posted on 2011-09-29
2
Medium Priority
?
1,500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 …
Six Sigma Control Plans
Suggested Courses

764 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