当我使用SQLiteHelper从本地数据库填充它时,我的RecyclerView工作正常,但现在我想使用SQLiteAssetHelper使用位于我的资源文件夹中的外部数据库填充它,所以我按照此{{3}的示例进行操作但我认为SimpleCursorAdapter()方法兼容ListView但不与RecyclerView兼容,因为它给我一个“不兼容类型”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类:
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
}
}
答案 0 :(得分:2)
<?php
$link="https://secure.example.org/Directory/AnotherDirectory/0008/102/12.jpg";
echo preg_replace("/([^\/]+)$/", 'L$1', $link);
是SQLiteAssetHelper
的即插即用替代品,因为SQLiteOpenHelper
本身会延伸SQLiteAssetHelper
。
因此,鉴于代码适用于SQLiteOpenHelper
的子类,您需要做的就是:
将该子类更改为扩展SQLiteOpenHelper
远程SQLiteAssetHelper
和onCreate()
方法
如果需要,调整对超类构造函数的调用
然后,根据onUpgrade()
按the documentation打包数据库,你应该很高兴。
在您展开assets/
时,任何有效的代码都应继续工作。
答案 1 :(得分:-1)
您可以将数据库从Assets文件夹复制到&#34;默认&#34;使用此方法的位置:
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包围。