I have a fragment that needs to connect to the database to view its contents and in order to do that I need to get a cursor to my database. Now I have a database class as "DB" and a fragment class "FRAG" . To get a cursor I need to call certain methods of "DB" class inside my fragment. Now to call a method of "DB" class I need a object of "DB" class, the problem is that I am unable to initialize a constructor of "DB" class in fragment class. Syntax is as follows-
Original database constructor is
public DB(Context context, String name, CursorFactory factory,int version) {
super(context,"database", factory,1);
}
I am initializing its object in fragment as follows-
DB ob=new DB(this,"database",null,1);
It gives me a syntax error as -
The constructor DB(Frag, String, null, int) is undefined
How do I initialize a object in this case. Kindly help.
Activity is a descendant of the Context class, not the Fragment.
You have to call the constructor with:
new DB(this.getActivity(),"database",null,1);
where this refers to your Fragment
Related
I want to define a class with just one constructor with body . When i set parameter of class i can't define the body of primary constructor.
I want the class have only one constructor.
Foe example
I have a class that extends CursorWrapper. This class have parameter that need to superclass implementation. I need a constructor with one parameter that do it:
class wrapper(cursoe: Cursor): CursoreWrapper {
// i need to call super(cursor). But where?
}
If you want to call cursor, it means your CursorWrapper (clearly typo here) is at least an abstract class, and not an interface.
Judging by your question, it also receives cursor (another typo) as an argument, so we get something like this:
open class CursorWrapper(cursor: Cursor)
Now, to invoke this constructor, you simply call it when you inherit from the class:
class Wrapper(cursor: Cursor): CursorWrapper(cursor) // <- this is your super(cursor)
Now let's assume that you also have another function on CursorWrapper that you need to call during construction.
open class CursorWrapper(cursor: Cursor) {
fun bla() {
// You want to call this for some reason as super.bla()
}
}
You can use init() block for that.
class Wrapper(cursor: Cursor): CursorWrapper(cursor) {
// i need to call super(cursor). But where?
init {
super.bla()
}
}
I need to create an instance of a class that extends SQLiteOpenHelper. I cannot create an empty constructor within the class that extends SQLiteOpenHelper, so how do I initialize the instance in my other class?
For example, I cannot just do this:
MyDatabaseManager myDatabaseManager = new MyDatabaseManager();
because the constructor of MyDatabaseManager requires a context, name, factory, and version as parameters, but I also get an error when I try to create an empty construcor within the MyDatabaseManager class, because it extends SQLiteOpenHelper.
So how would I go about creating an instance of that class in another class?
In the docs, it says I should make the new class like this:
class MyView extends GLSurfaceView {
public MyView(Context context) {
super(context);
setRenderer(renderer);
}
}
Now I tried to re-do that in Scala:
class BaseGameActivity extends Activity {
object glview extends GLSurfaceView(this) {
setRenderer(renderer)
setEGLContextClientVersion(2)
}
}
However, the App crashes now with the exception "java.lang.IllegalStateException: setRenderer already called for this instance". I suspect this has to do with the way Scala calls the super-constructor.
I've tried to find out how to override the constructor in the way the docs describe, but couldn't find it. I'd appreciate any hint.
It seems to me that your are propagating the call to a different constructor from the base class. You are passing a reference to this instead of a reference to the Context object. It might be that this other constructor is calling setRenderer.
Could you try to create an inner class MyGLView like this:
class MyGLView(ctx: Context) extends GLSurfaceView(ctx) {
setRenderer(renderer)
}
And see what happens?
The problem is that object does not allow arguments to its constructor. Top-level objects must be initializable without any arguments (nobody calls their ctors). In your case you have an inner object, which can reference the members of the surrounding class instance. If you really need an inner object in your Activity class, you could do:
object glview extends GLSurfaceView(ctx) {
setRenderer(renderer)
}
where ctx is a member of the surrounding class.
In java likewise in scala constructors are not inherited.
So you can not override thing, you didnt inherit. And you should use one of existing constructors for base class. If all of them are calling setRenderer(renderer) it will be called during constructing super object and you obviously should not call it second time in a subtype constructor ( wheither it class, object or mixing-in trait ).
I am extending the SQLiteOpenHelper class. My constructor is
public MyDatabaseHelper(Context context) {
super(
context, // ???
"MyDatabase.db", // Database name
null, // Cursor factory
1 // database version
);
}
What does the SQLiteOpenHelper constructor do with the context information?
For my application, the constructor will behave the same regardless of the program state (context). Can I pass null in for the context with out any future problems?
If you supply a null value, it will create an in-memory database instead but you'll need to supply null for the database name parameter as well so it's handled properly.
This is documented in the constructor documentation for context
context to use to open or create the database name of the database
file, or null for an in-memory database
Also, if you view the source code of the SQLiteHelper class itself, you will see it uses the mName value to decide whether to use mContext. View the source code online here:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.4_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.0mContext
DBHelper db =new DBHelper(context);
Cursor result = db.weeklyMedicinesTaken();
//WeeklyMedicinesTaken is a function in DBHelper class which returns cursor containing an integer value.
startManagingCursor(result); //this function is a resistance in what i want to do.
while(result.moveToNext())
{
int count=result.getInt(0);
}
result.close();
Actually I have simple classes (non activity classes) in which i want to retrieve data from sqlite and apply some processing and evaluation of data., but the problem is that the code above is working fine in activity but not working in any non activity class or in any static function of activity so that i can call that function from any class.
Any Suggestion Please??
Use getActivity().startManagingCursor(c) or pass an instance of your context to the class inwhich you want to call startManagingCursor()
Lets say your class is sth like this:
Person{
Context mContext;
String name, surname;
Person (Context context){
mContext = context;
}
While creating your Person object you should pass the context like this:
in your onCreate() or somewhere else inside the activity:
Person p = new Person(getActivity());
However, it's not a good practice to manage your cursor outside the activity.
You can examine this tutorial for simple patterns.