Search code examples
androidsqlitesqliteopenhelper

Can you extend 2 things in an activity on android studio? Using getreadabledatabase outside SQLiteopenhelper


Hey guys so i want to use getReadableDatabase() in my main activity but the problem is that it is already extending appCompatActivity() and i need it to do that for my code. But now since i cant extend 2 things at a time ,i cant extend SQLiteOpenHelper and that leads to not being able to use getreadable. Can anyone help me with this issue?


Solution

  • You need to define create a second class that extends SQLiteOpenHelper and define methods within there that use getReadableDatabase(). You can set up SQLiteOpenHelper as a singleton so that you can access a single instance of the class from anywhere.

    See http://blog.anorakgirl.co.uk/2018/01/refactoring-sqliteopenhelper-so-it-isnt-a-mile-long/ for how you can structure your SQLiteOpenHelper class.