Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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 …

746 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

8 Experts available now in Live!

Get 1:1 Help Now