Why is Context used in this piece of code? - android

public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
Here in this code,what does this Context means and what is the purpose of using this?

Context is an abstract Class that is used to access system resources like the network connection, or the file system or a database. In this case I would asume that the context is saved in the Adapter to load Images later in the progress.

Related

Access Context from another activty in Android

I have a Main class and another two classes named WebServicesClass and DynamicHeightAdpater.
I am creating an instance of DynamicHeightAdpater in WebServicesClass for which I need the context of MainActivity but I am not sure how to point it. The way by which I am calling throws a NullPointerException.
CODE :
MainActivity :
static Context context;
context = this.context;
WebServicesClass :
new DynamicHeightAdapter(MainActivity.context, 1, rowItems);
But it throws a null pointer exception and I am sure that it is due to the context cause I tried to print it and it threw NullPointer.
I would suggest you to take a look at the Android application class. You can store context there and retrieve it, when needed:
public class TestApplication extends Application {
private static Context mAppContext;
#Override
public void onCreate() {
super.onCreate();
mAppContext = getApplicationContext();
}
/**
* Returns the application's context. Useful for classes that need a Context
* but don't inherently have one.
*
* #return application context
*/
public static Context getAppContext() {
return mAppContext;
}
I suggest passing the Context to the WebServicesClass object in it's constructor, and having a member variable to keep it... something like
public class WebServicesClass
{
private Context mContext;
...
public WebServicesClass(Context c) // constructor
{
mContext = c;
}
void someOtherFunction()
{
new DynamicHeightAdapter(mContext, 1, rowItems);
}
}

how to handle context for this specific java class in Android

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.

Trouble with Constructor with helper

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.

How can i reach getResources() and Context except Activity class?

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;
}

Manage DbAdapter in custom SipmleCursorAdapter

I have class MyCustomAdapter
public class MyCustomAdapter extends SimpleCursorAdapter{
}
I have other class called DbAdapter, where I have functions with sql query. I'd like to access functions from class MyCustomAdapter
DbAdapter db and next I have to add
db = new DbAdapter(this)
but it doesnt work. I tried
Contex contex
db = new DbAdapter(contex) but then I have java.lang.NullPointerException
Is any way to access my DbAdapter within MyCustomAdapter?
Make your MyCustomAdapter-class accept a Context-object as a parameter:
public class MyCustomAdapter extends SimpleCursorAdapter{
private final Context context;
public MyCustomAdapter(Context c){
this.context = c;
}
}
In the calling activity, you can then simply pass this for the context.
You're getting this error because the DVM tries to invoke a SimpleCursorAdapter(Context)-constructor, which does not exist.
The solution to your problem is either explicitly calling a valid constructor of SimpleCursorAdapter by using super(...) or making the MyCursorAdapter-constructor accept the same parameters as one of the SimpleCursorAdapter-constructors.
What way to go relies on what you want to do with your Cursor-adapter. If you have constant values for certain constructor-arguments, you can use those, if you don't, you'll need to include them in your MyCursorAdapter-constructor.
As an example, this assumes you don't have constant values for any constructor-argument:
public class MyCustomAdapter extends SimpleCursorAdapter{
private final Context context;
public MyCustomAdapter(Context context, int layout, Cursor c, String[] from, int[] to){
super(Context context, int layout, Cursor c, String[] from, int[] to); // Call the super-
// classes constructor
this.context = context; // Save the context for further use
}
}
Some further information on this might be found here.

Categories

Resources