简体   繁体   中英

Populate a RecyclerView using SQLiteAssetHelper

My RecyclerView was working fine when i populated it from a local database using SQLiteHelper, but now i want to populate it using an external database located in my assets folder by using SQLiteAssetHelper, so i followed the example from this Github SQLiteAssetHelper example but i think that the SimpleCursorAdapter() method is compatible ListView but not with the RecyclerView as it gives me an "Incompatible Type" IDE error.

Is their a way around this or should i convert my RecyclerView to a ListView? Help would be greatly appreciated.

public class DisplayActivity extends AppCompatActivity {

    DataSource mDataSource;
    List<Facility> facilityList = DataProvider.facilityList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mDataSource = new DataSource(this);
        mDataSource.open();                             // Open database
        mDataSource.seedDatabase(facilityList);

        List<Facility> listFromDB = mDataSource.getAllItems();
        FacilitiesAdapter adapter = new FacilitiesAdapter(this, listFromDB);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.displayActivityRecyclerView);
        recyclerView.setAdapter(adapter);

    }   // End of onCreate()


    @Override
    protected void onPause() {
        super.onPause();
        mDataSource.close();                        // Close database connection when application is
    }                                               // paused to prevent database leaks.

    @Override
    protected void onResume() {
        super.onResume();
        mDataSource.open();                         // Open database connection when application is resumed
    }
}

SQLiteHelper Class:

public class DBHelper extends SQLiteOpenHelper {

    public static final String DB_FILE_NAME = "health.db";
    public static final int DB_VERSION = 1;

    public DBHelper(Context context) {
        super(context, DB_FILE_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(FacilitiesTable.SQL_CREATE);         // Execute SQL_CREATE statement;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(FacilitiesTable.SQL_DELETE);         // delete old database
        onCreate(db);                                   // Create new database
    }
}

SQLiteAssetHelper is a plug-and-play replacement for SQLiteOpenHelper , as SQLiteAssetHelper itself extends SQLiteOpenHelper .

So, given code that works with a subclass of SQLiteOpenHelper , all you need to do is:

  • Change that subclass to extend SQLiteAssetHelper

  • Remote the onCreate() and onUpgrade() methods

  • Adjust the call to the superclass constructor, if needed

Then, package your database in assets/ , per the documentation , and you should be good to go.

Any code that worked when you were extending SQLiteOpenHelper should continue to work now that you are extending SQLiteAssetHelper .

You can copy the database from Assets folder to "default" location using this method:

public final String path = "/data/data/YourPackageName/databases/";
    public final String Name = "DataBaseName.db";

    public void copydatabase() throws IOException {

        OutputStream myOutput = new FileOutputStream(path + Name);
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = getApplicationContext().getAssets().open("DataBaseName.db");
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }
        myInput.close();
        myOutput.flush();
        myOutput.close();

    }

Than just call :

copydatabase() 

And surround with try/catch.

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