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
Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…

856 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