Accessing "Context" with other classes

Put your problem here if it does not fit any of the other categories.

Accessing "Context" with other classes

Postby theblitz » Wed Mar 30, 2011 11:57 pm

When calling SQLite I need to pass it the current context.
However, I am working not from the base Activity class but from an instantiation of classes of my own making.
How do i get to the context?

Should I be passing the context as a parameter to each of my classes?
theblitz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 18, 2011 9:11 am

Top

Re: Accessing "Context" with other classes

Postby vhalexxs » Fri Apr 01, 2011 9:01 am

Hi theblitz,

Just add the Context on the class's constructor,

Ex.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Class1 {
  2.     private Context ctx;
  3.  
  4.     public Class1(Context ctx) {
  5.         this.ctx = ctx; // you can then use ctx on other method of the class
  6.         // Your code that is needed to use Context goes here
  7.     }
  8. }
  9.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Alex Escobar
Senior Android Engineer
-----------------------
Airpush.com: The Push Notification Ad Network
User avatar
vhalexxs
Developer
Developer
 
Posts: 28
Joined: Mon Mar 14, 2011 3:19 am

Re: Accessing "Context" with other classes

Postby theblitz » Fri Apr 01, 2011 9:48 am

Thx.
Is it safe to pass the Activity Context or should I pass the Application Context?
theblitz
Junior Developer
Junior Developer
 
Posts: 22
Joined: Fri Feb 18, 2011 9:11 am

Re: Accessing "Context" with other classes

Postby strider2023 » Mon Apr 04, 2011 6:56 am

You can use both...but I personnaly prefer Acitvity Context. :)
strider2023
Senior Developer
Senior Developer
 
Posts: 126
Joined: Tue Mar 02, 2010 6:34 am
Location: Chennai, India

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests