使用 SQLiteAssetHelper 填充 RecyclerView

Populate a RecyclerView using SQLiteAssetHelper

当我使用 SQLiteHelper 从本地数据库填充它时,我的 RecyclerView 工作正常,但现在我想使用 SQLiteAssetHelper 使用位于我的资产文件夹中的外部数据库来填充它,所以我按照这个 Github SQLiteAssetHelper example 但我认为 SimpleCursorAdapter() 方法与 ListView 兼容,但与 RecyclerView 不兼容,因为它给我一个 "Incompatible Type" IDE 错误。

他们有办法解决这个问题,还是我应该将我的 RecyclerView 转换为 ListView?将不胜感激。

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
    }
}

您可以使用以下方法将数据库从 Assets 文件夹复制到 "default" 位置:

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();

    }

而不只是打电话:

copydatabase() 

并用 try/catch 包围。

SQLiteAssetHelperSQLiteOpenHelper 的即插即用替代品,因为 SQLiteAssetHelper 本身扩展了 SQLiteOpenHelper.

因此,给定与 SQLiteOpenHelper 的子类一起工作的代码,您需要做的就是:

  • 更改该子类以扩展 SQLiteAssetHelper

  • 远程 onCreate()onUpgrade() 方法

  • 如果需要,调整对超类构造函数的调用

然后,根据 the documentation,将您的数据库打包到 assets/ 中,您应该可以开始了。

任何在您扩展 SQLiteOpenHelper 时有效的代码在您扩展 SQLiteAssetHelper 时应该继续有效。