我正在创建一个名为motodescriptive的应用程序,并使用room数据库来存储我的数据(摩托车的名称、描述和URL)。在我的模拟器上一切都运行得很好,但是昨天我试着在手机上运行这个应用程序,而recyclerview完全是空白的,可能是因为一个空的数据库。你可以在下面查我的代码,提前谢谢。
这是我的appdatabase.java文件
package com.example.motodescriptive;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
@Database(entities = {MotoEntity.class}, version = 14)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase instance;
private static String NAME = "moto-database";
public abstract MotoDao motoDao();
public static synchronized AppDatabase getInstance(Context context) {
if(instance == null) {
instance = Room.databaseBuilder(context, AppDatabase.class, NAME).fallbackToDestructiveMigration().allowMainThreadQueries().addMigrations(MIGRATION_13_14).build();
}
return instance;
}
static final Migration MIGRATION_13_14 = new Migration(13, 14) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
}
};
}
motodao.java文件
package com.example.motodescriptive;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface MotoDao {
@Query("SELECT * FROM motoentity")
List<MotoEntity> selectAll();
@Query("SELECT * FROM motoentity WHERE moto_name LIKE :name")
List<MotoEntity> checkForName(String name);
@Query("SELECT COUNT(*) FROM motoentity where moto_name = :name")
int agentsCount(String name);
@Insert
void insert(MotoEntity motoEntity);
@Update
void update(MotoEntity motoEntity);
@Delete
void delete(MotoEntity motoEntity);
@Query("DELETE FROM motoentity where ID = :motoid")
void deleteById(int motoid);
}
motonentity.java文件
package com.example.motodescriptive;
import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.io.Serializable;
@Entity
public class MotoEntity implements Serializable {
@PrimaryKey(autoGenerate = true)
public int ID;
@ColumnInfo(name = "moto_name")
public String moto_name;
@ColumnInfo(name = "moto_desc")
public String moto_desc;
@ColumnInfo(name = "moto_longdesc")
public String moto_longdesc;
@ColumnInfo(name = "moto_longdesc2")
public String moto_longdesc2;
@ColumnInfo(name = "moto_longdesc3")
public String moto_longdesc3;
@ColumnInfo(name = "moto_longdesc4")
public String moto_longdesc4;
public String getMoto_longdesc2() {
return moto_longdesc2;
}
public void setMoto_longdesc2(String moto_longdesc2) {
this.moto_longdesc2 = moto_longdesc2;
}
public String getMoto_longdesc3() {
return moto_longdesc3;
}
public void setMoto_longdesc3(String moto_longdesc3) {
this.moto_longdesc3 = moto_longdesc3;
}
public String getMoto_longdesc4() {
return moto_longdesc4;
}
public void setMoto_longdesc4(String moto_longdesc4) {
this.moto_longdesc4 = moto_longdesc4;
}
public String getMoto_longdesc() {
return moto_longdesc;
}
public void setMoto_longdesc(String moto_longdesc) {
this.moto_longdesc = moto_longdesc;
}
@ColumnInfo(name = "moto_img")
public String moto_img;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getMoto_name() {
return moto_name;
}
public void setMoto_name(String moto_name) {
this.moto_name = moto_name;
}
public String getMoto_desc() {
return moto_desc;
}
public void setMoto_desc(String moto_desc) {
this.moto_desc = moto_desc;
}
public String getMoto_img() {
return moto_img;
}
public void setMoto_img(String moto_img) {
this.moto_img = moto_img;
}
}
最后,在这里,我将数据库中的数据添加到一个arraylist中,并将其传递给适配器。我把我的数据放到了数据库里。因为这部分代码已经存储在数据库中,所以我不再需要它了,所以我删除了它。
List<MotoEntity> motorcycles = appDatabase.motoDao().selectAll();
adapter = new MotorAdapter((ArrayList<MotoEntity>) motorcycles, this, getApplicationContext());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
暂无答案!
目前还没有任何答案,快来回答吧!