my question is simple. How can i get access to the string value in res/values/string.xlm in a extends SQLiteOpenHelper because getString method is use with an Activity ?
u can get context object through constructor.
class DatabaseHelper extends SQLiteOpenHelper {
private Context mContext;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
}
Try
Receive Context in your SQLiteOpenHelper class and then use Context like
context.getResources().getString(R.string.yourStringNameFromStrings);
You need a reference to Context. The constructor of SQLiteOpenHelper accepts a context, you can save it in a local variable. If c is context variable, and myString is the name of string resource in strings.xml, then you can get string value as:
C.getString(R.string.myString);
Related
I'm getting the
'This class should provide a default constructor'
error when i'm trying to build the APK
this is my DBHelper class:
public class DBHelper extends SQLiteOpenHelper {
// create variables
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
// onCreate
I was under the impression that
public DBHelper(Context context)
was the default constructor? And have checked other answers with this and can't find anything to help...
Thanks in advance
A default constructor is a constructor without any arguments.
SQLiteOpenHelper needs at least a context so you won't be able to create a default constructor for your DBHelper. Are you sure this is the class causing this error ?
As the error suggest
Error: This class should provide a default constructor (a public constructor with no arguments)
try this way:
public class DBHelper extends SQLiteOpenHelper {
// create variables
public DBHelper(){
super();
}
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, 1);
}
// onCreate
If multiple activities within an app call the constructor of my SQLiteOpenHelper with themselves as the context argument, can I be sure that they will all access the same database?
For example, let's say I have:
package foo.bar;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = new MySQLiteOpenHelper(this).getReadableDatabase();
:
}
}
and
package foo.bar.baz;
public class Activity2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase db = new MySQLiteOpenHelper(this).getReadableDatabase();
:
}
}
Here's in the skeleton of my SQLiteOpenHelper subclass:
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "comments.db";
private static final int DATABASE_VERSION = 1;
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
:
}
I can't find anything in the documentation that guarantees that the two activities get the same database. On the other hand, I haven't seen any mentions of people getting different databases from contexts within a single application. Are the database paths guaranteed to be identical?
SQLiteOpenHelper has 2 constructors and the second parameter for both of them is the database file name.
If you used the same database file name when using SQLiteOpenHelper from different activities, you will get access to the same database.
This is usually taken care of in the constructor of the inheriting class you create - DATABASE_NAME is a constant:
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
if you do it as the Android folks recommend (http://developer.android.com/guide/topics/data/data-storage.html#db)
Then yes, all activities within an app will see the same database
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 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.
i have created class cl_DBHandlerwhich extends SQLiteOpenHelper. and i have created class my_srvcwhich extends service. i have intialise object m_DBHandlerof class cl_DBHandlerin service but while initialization object i have to pass parameters to cl_DBHandler(context, name, factory, version), i am not getting exactly what to pass in this.
i have pass like this cl_DBHandler(getappcontext(),"databasename.sqlite",null,1)
2nd thing: i am calling one function from class with help of m_DBHandler object but i am getting value null of object m_DBHandler at every call though i have intialise that object in service.can anyone tell me or guide me to solve this problem.
Thanks in Advance--
Use the following constructor for your cl_DBHandler class
public static final String DATABASE_NAME = "MyDBName.db";
public static final int DATABASE_VERSION = 1;
public cl_DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
In your service or activity:
cl_DBHandler m_DBHandler = new cl_DBHandler(this);
Then open your database:
SQLiteDatabase db;
try {
db = m_DBHandler.getWritableDatabase();
} catch (SQLiteException ex) {
db = m_DBHandler.getReadableDatabase();
}
// do anything you like for example:
Cursor cursor = db.rawQuery(strQuery, null);