Я создал базу данных, содержащую несколько таблиц.
У меня есть база данных, сохраненная в папке активов.
Я пытаюсь просто получить доступ к EXERCISESTABLE из этой базы данных, а затем отобразить его значения в модифицированный listView. Тем не менее, я получаю сообщение об ошибке:
W/System.err: java.io.FileNotFoundException.
Вот мой MainActivity расширяет класс деятельности:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvExercise = (ListView)findViewById(R.id.listView_exercises);
mDBHelper = new DatabaseHelper(this);
//Check exists database
File database = getApplicationContext().getDatabasePath(DatabaseHelper.DBNAME);
if(false == database.exists()){
mDBHelper.getReadableDatabase();
// Copy Database
if(copyDatabase(this)){
Toast.makeText(this, "Copy database success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Copy data error", Toast.LENGTH_SHORT).show();
return;
}
}
//Get exercise List in db when db exists
mExercisesList = mDBHelper.getListExercise();
//Init adapter
adapter = new ListExerciseAdapter(this, mExercisesList);
//Set adapter for list view
lvExercise.setAdapter(adapter);
}
private boolean copyDatabase(Context context){
try{
InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[]buff = new byte[1024];
int length =0;
while ((length = inputStream.read(buff)) >0){
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w("MainActivity", "DB copied");
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
У меня есть файл базы данных workout_program.db, расположенный в папке активов, однако я Кажется, я не могу получить к нему доступ, хотя все выглядит правильно для меня.
Вот мой DBHelper расширяет SQLiteOpenHelper Код:
private Context mContext;
private SQLiteDatabase mDatabase;
public DatabaseHelper(Context context){
super(context, DBNAME, null, 1);
this.mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void openDatabase() {
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if(mDatabase != null && mDatabase.isOpen()){
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath,null,SQLiteDatabase.OPEN_READWRITE);
}
public void closeDatabase(){
if(mDatabase!=null){
mDatabase.close();
}
}
public List<Exercise> getListExercise(){
Exercise exercise = null;
List<Exercise> exerciseList = new ArrayList<>();
openDatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM EXERCISESTABLE ", null);
cursor.moveToFirst();
while(!cursor.isAfterLast()){
exercise = new Exercise(cursor.getInt(0),cursor.getString(1), cursor.getInt(2), cursor.getInt(3));
exerciseList.add(exercise);
cursor.moveToNext();
closeDatabase();
}
return exerciseList;
}
Вот мой класс упражнений:
public class Exercise {
private int exercise_id;
private String exercise_name;
private int exercise_type_id;
private int exercise_equipment_id;
public Exercise(int exercise_id, String exercise_name, int exercise_type_id, int exercise_equipment_id) {
this.exercise_id = exercise_id;
this.exercise_name = exercise_name;
this.exercise_type_id = exercise_type_id;
this.exercise_equipment_id = exercise_equipment_id;
}
public int getExercise_id() {
return exercise_id;
}
public void setExercise_id(int exercise_id) {
this.exercise_id = exercise_id;
}
public String getExercise_name() {
return exercise_name;
}
public void setExercise_name(String exercise_name) {
this.exercise_name = exercise_name;
}
public int getExercise_type_id() {
return exercise_type_id;
}
public void setExercise_type_id(int exercise_type_id) {
this.exercise_type_id = exercise_type_id;
}
public int getExercise_equipment_id() {
return exercise_equipment_id;
}
public void setExercise_equipment_id(int exercise_equipment_id) {
this.exercise_equipment_id = exercise_equipment_id;
}
}
Вот мой ListExerciseAdapter расширяет класс BaseAdapter:
public ListExerciseAdapter(Context mContext, List<Exercise> exercisesList) {
this.mContext = mContext;
this.mExercisesList = exercisesList;
}
@Override
public int getCount() {
return mExercisesList.size();
}
@Override
public Object getItem(int position) {
return mExercisesList.get(position);
}
@Override
public long getItemId(int position) {
return mExercisesList.get(position).getExercise_id();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = View.inflate(mContext, R.layout.exercises_listview_layout, null);
TextView exercise_name = (TextView)v.findViewById(R.id.exercise_name);
TextView exercise_type_id = (TextView)v.findViewById(R.id.exercise_type_id);
TextView exercise_equipment_id = (TextView)v.findViewById(R.id.exercise_equipment_id);
exercise_name.setText(mExercisesList.get(position).getExercise_name());
exercise_type_id.setText(String.valueOf(mExercisesList.get(position).getExercise_type_id()));
exercise_equipment_id.setText(String.valueOf(mExercisesList.get(position).getExercise_equipment_id()));
return v;
}
Любая помощь будет принята с благодарностью.