简体   繁体   中英

Accessing an inner subclass with private class constructor from another class

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?

如果您真的想保留您的内部类,则使其成为静态并按如下方式使用它:

new MyDBHandler.FeedReaderDbHelper(getContext())

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

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)

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:

  1. Some piece of functionality in your outer class would be most clear if it was implemented in a separate class.
  2. Even though it's in a separate class, the functionality is very closely tied to way that the outer class works.

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");
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM