I am Having a DataBase Helper class which is not an activity. Here I want to show a Toast that require a context. How can I pass a context from Another Activity?
Create Object of The Helper class from your Activity and pass 'this' as the context, say
MyHelperclass helper=new MyHelperclass(this);
In the Helper class Get this Context via its constructor
Context context;
MyHelperClass(Context context){
this.context=context;
}
Now You can pass this context to the makeText() method of Toast class.
You can create a method like setContext(Context context) in your DataBase Helper class to pass your context from your activity.
You can get the context if you do getApplicationContext(); from your activity and pass that to your DBHelper class.
Related
I'd like to get my string-array without extending Activity in my custom class. Is there a way to do this?
String[] foo_array = getResources().getStringArray(R.array.foo_array); will not work without extending Activity, so I need a work-around.
Pass the context to the constructor of custom class and use the same
new CustomClass(ActivityName.this);
Then
Context mContext;
public CustomClass(Context context)
{
mContext = context;
}
use the context
String[] foo_array = mContext.getResources().getStringArray(R.array.foo_array);
Also keep in mind
Do not keep long-lived references to a context-activity (a reference to an activity should have the same life cycle as the activity itself)
http://android-developers.blogspot.in/2009/01/avoiding-memory-leaks.html
Also check this
android getResources() from non-Activity class
Edit:
Change this
public class CustomClass(Context context)
{
}
To
public class CustomClass
{
Context mContext;
public CustomClass(Context context) // constructor
{
mContext = context;
}
}
try this,
Context context=getApplicationContext();
String[] foo_array = context.getResources().getStringArray(R.array.foo_array);
And, do not use Activity Context as that is tied to the Activity life cycle.
Update,
getApplicationContext() is from Context class. That means any thing extended Context have this method. This also means you will be able to use this from service or from other resources.
But, if you custom class do not extend Activity/context, you have to pass Context as parameter to use getApplicationContext()
if you declare your activity like this
myMethod(Activity activity) //this is bad
Bud if it is like following,
myMethod(Context context) //this is ok
but from above declaration do not pass Activity or Service Context as they have own life cycle. instead you will use getApplicationContext()
You need pass the Activity context to the Custom class.
private Context context;
public CustomClass(Context context)
{
this.context=context;
}
if you use numberpicker and pass String from sring xml then use this
np_Basic_Hight.setMinValue(0);
np_Basic_Hight.setMaxValue(71);
np_Basic_Hight.setDisplayedValues(getContext().getResources().getStringArray(R.array.hieght));
The deleteDatabase method needs a context in order to work. So in my class there is a context declared called ourContext. This class does not extend any other class like
Activity so I guess you could call it a helper class.
The only place in the class that uses context is the one method shown below that is called deleteData. and this calls the deleteDatabase method that needs a context to work.
ourContext.deleteDatabase(DATABASE_NAME);
Is it possible to not declare a context for the class in this situation? Can I use this for the context?
public class PlayGame {
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
public PlayGame(Context c){
ourContext = c;
}
public void deleteData(){
ourContext.deleteDatabase(DATABASE_NAME);
}
No, Context is a class, you have to be derived from it to use this as a context. I suggest making deleteData take a Context as a parameter. I assume its going to be called from an Activity, Service, or something like a view that has a reference to a Context.
VPAdapter.java
public class VPAdapter extends PagerAdapter
{
public static String[] titles;
public final Context context;
public int[] scrollPosition;
JSONArray categories = null;
JSONArray newstype = null;
JSONObject json;
DatabaseHandler db = new DatabaseHandler(context)//error:The blank final field context may not have been initialized
...
}
DatabaseHandler.java
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
On VPAdapter.java I wanted to access DatabaseHandler anywhere, but there is problem with the constructor. What is the proper way I should write them?
You cannot pass a variable that was not initialize.
On your second line of the function you declare the context variable but you don't assign any value to it.
The last line should be written in the constructor of VPAdapter. The constructor should get a context variable. When you call your constructor you probably want to use the application context, but you might send also an activity (Activity inherit from context) but this is usually not recommended (But it really depends on your code)
Because your Context is null first initialize your context than you can pass that context to your database handler constructor.
Context context = getApplicationContext();
Or try below code
For example initialize your Context with your activity context.
Create constructor of your APAdapter class and call that constructor from your activity. Same way as you create for database handler.
public APAdapter(Context context) {
this.context = context;
}
than pass that context to your database handler.
I know this question is general but I am always face to face this problem.
My question is I can reach getResource() or getContext() in Activity but when I want to use a class without activity
example:
public class MapOverlay extends ItemizedOverlay
I can not reach getResources() or Context.
How can I do that anybody know any trick?
I usually do it the following way:
Create a class that extends Application, say MyApp.
Declare a private static Context context field
Declare a (static) getter for the context field
Initialize the field in onCreate(): context = this
Now context is available across all application via MyApp.context()
ItemizedOverlay in not extending android.content.Context.
You can create the construcor like
private Context mContext;
public MapOverlay(Context context){
this.mContext=context;
}
and then use the mContext field to call getResource() or getContext() methods.
Just expose a public method inside your MapOverlay that takes a Context.
public void setContext(Context context) {
mContext = context;
}
I'm defining a class that sets a Drawable attribute in an object. The problem is that I can't access the getResource().getDrawable(int resourceId) method unless I have some Context.
What I did was to send to that class an activity instance (let's call it "act") and then I did:
act.getResources().getDrawable(R.drawable.whellchair)
but, when executing that line it throws a NullPointerException.
When idea how to accomplish this?
I found the problem!
I'm using a singleton and I put the line accessing the "act" in a static method... how fool of me ...
Sorry and thank you Juhani for the comment :)
Pass that the application context to the constructor of your class. In the main application class you just get the context by invoking the getApplication() method if you need the a lifetime aware context or getApplicationContext() if you need a the context which is tied to the current process.
Example:
private Context ctx = getApplication();
... some code ...
MyClass myClass = new MyClass(ctx);
Your classes' constructor of course has to handle the context accordingly (i.e. setting a private member of type Context to the passed value) like this.
private Context ctx = null;
public MyClass(ctx) {
this.ctx = ctx;
}
Then you can use the context for whatever you need.