SQLiteHelperを使用してローカルデータベースからデータを入力したときにRecyclerViewが正常に動作しましたが、SQLiteAssetHelperを使用して資産フォルダにある外部データベースを使用してデータを入力しますこれはGithub SQLiteAssetHelper exampleですが、SimpleCursorAdapter()メソッドは互換性のあるListViewだと思いますが、RecyclerViewではなく、「互換性のないタイプ」のIDEエラーが発生します。SQLiteAssetHelperを使用してRecyclerViewにデータを入力する
これを回避する方法、または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
}
}
"私のRecyclerViewは、SQLiteHelperを使用してローカルデータベースからデータを入力したときに正常に動作していました" - そこでは 'SimpleCursorAdapter'を使用していませんでした。それで、 "うまくいきました"ならば、そのコードにスティックして 'SQLiteAsHelper'を' SQLiteOpenHelper'に置き換えてください。それ以外の場合は、 'Cursor'をデータソースとして使用する独自の' RecyclerView.Adapter'を作成してください。 – CommonsWare
@CommonsWareもう一度返信いただきありがとうございます:)しかし、私は外部データベースを使用するか、何らかの形でデータベースファイルを/ data/data/package/databasesフォルダにコピーする必要があります。 – aatj
あなたの質問を編集し、 "SQLiteHelperを使用してローカルデータベースからデータを入力して正常に動作しています"というコードを表示してください。 – CommonsWare