Reputation: 383
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?
Upvotes: 2
Views: 196
Reputation:
The inner class is just a way to cleanly separate some functionality that really belongs to the original outer class. They are intended to be used when you have 2 requirements:
Given these requirements, inner classes have full access to their outer class. Since they're basically a member of the outer class, it makes sense that they have access to methods and attributes of the outer class -- including privates
followings are the way:
new MyDBHandler.FeedReaderDbHelper(getContext())
I have run your code by following way, Please have a look.
public class MyDBHandler {
public static FeedReaderDbHelper feedReaderDbHelper;
private MyDBHandler() {
}
public static FeedReaderDbHelper getFeedReaderDbHelper()
{
if(feedReaderDbHelper == null)
feedReaderDbHelper = new MyDBHandler(). new FeedReaderDbHelper();
return feedReaderDbHelper;
}
public static class FeedEntry {
public static final String TABLE_NAME = "Tasks";
public static final String COLUMN_NAME_TITLE = "TASK_LABEL";
}
public class FeedReaderDbHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "myDatabase.db";
public FeedReaderDbHelper() {
//super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public long saveTasks(String title) {
System.out.println(title);
return 1L;
}
}
public static void main(String[] args) {
MyDBHandler.getFeedReaderDbHelper().saveTasks("title");
}
}
Upvotes: 0
Reputation: 1082
You can make the inner class static to use it
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 static 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;
}
}
}
Now to get an instance of FeedReaderDbHelper you can
MyDBHandler.FeedReaderDbHelper dbHelper = new MyDBHandler.FeedReaderDbHelper(v.getContext());
and to call methods
String title = "your title";
dbHelper.saveTasks(title)
Upvotes: 1
Reputation: 2476
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
Upvotes: 1
Reputation: 7368
If you really want to keep your inner class then make it static and use it as follow:
new MyDBHandler.FeedReaderDbHelper(getContext())
Upvotes: 2