I'm creating SQL Database and i base on this site. The problem is, i don't know how can i access inner class' methods, while outer class has private constructor.
public class MyDBHandler {
private MyDBHandler() {
}
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "Tasks";
public static final String COLUMN_NAME_TITLE = "TASK_LABEL";
}
public class FeedReaderDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "myDatabase.db";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public long saveTasks(String title) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(FeedEntry.COLUMN_NAME_TITLE, title);
long newRowId = db.insert(FeedEntry.TABLE_NAME, null, contentValues);
return newRowId;
}
}
}
I'm trying to access saveTasks(String title) method from onClick(View v) method from an another java file.
I tried sth like this:
MyDBHandler.FeedReaderDbHelper dbHelper = new MyDBHandler.FeedReaderDbHelper(v.getContext());
but of course i get an "MyDBHandler is not an enclosing class" error.
OR
MyDBHandler.FeedReaderDbHelper dbHelper= new MyDBHandler().new FeedReaderDbHelper(v.getContext());
but again, Android studio keeps telling me: "MyDbHandler has a private access"
Is it even possible to do it that way?
MyDbHandler has a private access
This is not about the inner class, it is about the constructor in that class
private MyDBHandler() {
}
I guess I saw this guide (website) before and I didn't really know why they added it as an inner class. I remember they said to make the constructor private so no one can create an object out of it.
So simply move the FeedReaderDbHelper
to a separate file and then it will work.
But if for some reason you want to keep it inner class then define it as static
public static class FeedReaderDbHelper
Then call the same code and it should work