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.
Related
In android, I want to use a string-array resource to create a public static final String in one of my classes. However, it seems I must call getResources() on a Context and I can't find any context at the class level. Is this possible?
public class DBAdapter {
Resources res = getResources();
public static final String[] gradeTypes = res.getStringArray(R.array.gradeTypes);
...
You can't do that. If you need the Context, you can pass it with constructor:
public class DBAdapter{
private Context mContext;
public DBAdapter(Context mContext) {
this.mContext = mContext;
}
}
But anyway you won't have a chance to use it at the gradeTypes initialization. Why do you need to declare it as final? If you want to use this array at inner classes, you can do it without final modifier.
I have to make an application where I have to show a list of names in popup.
I have used array-list to fetch the values from database, but I cannot put it in array-adapter.
here is my code:
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
... // rest of the code
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(Calculator_new_Pop.this,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
... // rest of the code
}
the error is
"The constructor ArrayAdapter(Calculator_new_Pop, int, ArrayList) is undefined"
Can anyone help me out?
Use activity context
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(ActivityName.this,android.R.layout.simple_list_item_1,wallAreas);
You can pass the activity context to the constructor of Calculator_new_Pop and use the same instead of Calculator_new_Pop.this
Edit
Context mContext;
public Calculator_new_Pop(Context context)
{
mContext = context;
}
Then
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
Have a look at the constructor of ArrayAdapter.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
Refer the link
link
ArrayAdapter needs a context as a parameter, you are supplying Dialog instance which is not the Context type, this is the reason why an error is shown. Instead of Dialog instance get the activity context.
You can pass the activity context in the constructor of the dialog, and hence supply that context in the ArrayAdapter.
public class Calculator_new_Pop extends Dialog implements View.OnClickListener{
Context mContext =null;
public Calculator_new_Pop(Context c ){
this.mContext = c;}
.............
.............
.............
ArrayList<String> wallAreas=new ArrayList<String>();
wallAreas=GenericDAO.getWallAreas(room_id);//to fetch the values from databases
ArrayAdapter<String> new_adapter = new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,wallAreas);
_ltvw.setAdapter(new_adapter);
.......
}
When instantiating dialog from activity, pass this.
i am getting 'null' in "mContext"
so i tried this..
public Calculator_new_Pop(Activity parent) {
// TODO Auto-generated constructor stub
super(parent);
this._act = parent;
//
}
_ltvw.setAdapter(new ArrayAdapter<String>(_act,android.R.layout.simple_list_item_multiple_choice,data));
it worked..
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.
hi i am Using ArrayAdapter in ListView with custom Class object,HERE IS MY CODE
private static class NewsDetailAdapter extends ArrayAdapter<clsNewsItem>
{
private final Activity context;
List<clsNewsItem> newsList = null;
public NewsDetailAdapter(Activity context, ArrayList<clsNewsItem> clsNewsObjects) {
super(context, R.layout.listview_cell, clsNewsObjects);
this.context = context;
this.newsList = clsNewsObjects;
}
public void clear()
{
newsList.clear();
}
while i am working with this code AdapterObj.NotifyDatasetchanged() not working Due to i have not implemented addAll() method for this class,i cant understand how to write this Method so how can i Write Add All method for this ArrayAdaper class..can Any one help me please
Do you have specific need for subclassing? If you need to have some logic better have one ArrayAdapter as member instance and proxy its methods. Because the original ArrayAdapter already has all these methods readily available
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.