[英]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. 当我使用SQLiteHelper从本地数据库填充它时,我的RecyclerView运行良好,但是现在我想使用SQLiteAssetHelper使用位于我的资产文件夹中的外部数据库填充它,因此我遵循了这个Github SQLiteAssetHelper示例中的示例,但我认为SimpleCursorAdapter()方法与ListView兼容,但与RecyclerView不兼容,因为它给我一个“不兼容的类型” IDE错误。
Is their a way around this or should i convert my RecyclerView to a ListView? 他们是否可以解决这个问题,还是应该将RecyclerView转换为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: SQLiteHelper类别:
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
. SQLiteAssetHelper
是SQLiteAssetHelper
的即插即用替代SQLiteOpenHelper
,因为SQLiteAssetHelper
本身扩展了SQLiteOpenHelper
。
So, given code that works with a subclass of SQLiteOpenHelper
, all you need to do is: 因此,给定的代码可与
SQLiteOpenHelper
的子类SQLiteOpenHelper
,您所需要做的就是:
Change that subclass to extend SQLiteAssetHelper
更改该子类以扩展
SQLiteAssetHelper
Remote the onCreate()
and onUpgrade()
methods 远程
onCreate()
和onUpgrade()
方法
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. 然后,按照文档将数据库包装在
assets/
,您应该会很高兴。
Any code that worked when you were extending SQLiteOpenHelper
should continue to work now that you are extending SQLiteAssetHelper
. 现在,在扩展
SQLiteAssetHelper
时,扩展SQLiteOpenHelper
时起作用的任何代码都应继续SQLiteAssetHelper
。
You can copy the database from Assets folder to "default" location using this method: 您可以使用以下方法将数据库从Assets文件夹复制到“默认”位置:
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. 并用try / catch包围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.