gpt4 book ai didi

java - 房间数据库中的列表数组为空

转载 作者:太空宇宙 更新时间:2023-11-04 09:09:44 26 4
gpt4 key购买 nike

我正在使用 Room 持久性库从数据库获取数据,但它返回一个空数组 (nightclub_list)。

只需添加该片段为谷歌地图实现OnMapReadyCallback,并且getMapAsync位于onCreateView中。然而,房间数据库位于“onViewCreated”中。

我在这方面完全是初学者。

实体

@Entity(tableName = "nightclub")
public class NightClub {

// define variables including a non-null primary key (name)
@PrimaryKey
@NonNull
@ColumnInfo(name = "name")
public String name;

@ColumnInfo(name = "latitude")
public String latitude;

@ColumnInfo(name = "longitude")
public String longitude;

@ColumnInfo(name = "price")
public String price;

@ColumnInfo(name = "timeopen")
public String timeopen;

@ColumnInfo(name = "link")
public String link;

@ColumnInfo(name = "image_link")
public String image_link;


// definition of method getName
public String getName() {
return name;
}

// definition of void function to setName
public void setName(String club_name) { this.name = club_name; }


// definition of getLatitude method
public String getLatitude() {
return latitude;
}

// definition of setLatitude method
public void setLatitude(String club_latitude ) { this.latitude = club_latitude; }

// definition of getLongitude method
public String getLongitude() {
return longitude;
}

// definition of setLongitude method
public void setLongitude(String club_longitude) {
this.longitude = club_longitude;
}

// definition of getPrice method
public String getPrice() {
return price;
}

// definition of setPrice method
public void setPrice(String club_price) {
this.price = club_price;
}

// definition of getTimeOpen method
public String getTimeOpen() {
return timeopen;
}

// definition of setTimeOpen method
public void setTimeOpen(String club_timeopen) {
this.timeopen = club_timeopen;
}

// definition of getLink method
public String getLink() { return link; }

// definition of setLink method
public void setLink(String club_link) { this.link = club_link; }

// definition of getImage method
public String getImageLink() {
return image_link;
}

// definition of setImage method
public void setImageLink(String club_imagelink) {
this.image_link = club_imagelink;
}

}

DAO

@Dao
public interface NightClubDAO {

// This interface defines the operations that can be performed in the application from
// the database
@Insert
public void insert(NightClub nightClub);

@Update
public void update(NightClub nightClub);

@Delete
public void delete(NightClub nightClub);

@Query("SELECT * FROM nightclub")
List<NightClub> getAllNightClubs();


}

应用数据库

@Database(entities = {NightClub.class}, version = 3, exportSchema = false)

// this abstract class serves as a connector between the database and the application
public abstract class AppDatabase extends RoomDatabase {

public abstract NightClubDAO getNightClubDAO();

// database migration when schema (NightClub) java is altered
static final Migration MIGRATION_2_3 = new Migration(2, 3) {

@Override

public void migrate(SupportSQLiteDatabase database) {

}

};


}

map 片段

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

//============ All about the database
// get database path

// get context outside else it returns null


Context context = getContext();
final File dbFile = context.getDatabasePath(db_name);

// Log.d(TAG, "here are they: " + dbFile);


// check if database file exists
if (!dbFile.exists()) {
try {
copyDatabaseFile(dbFile.getAbsolutePath());
Log.d(TAG, "Reading file database successful");
} catch (IOException e) {
// Log.d(TAG, "Reading file database error: " + e);
e.printStackTrace();
}
}


AppDatabase database =
Room.databaseBuilder(context, AppDatabase.class, db_name)
.allowMainThreadQueries()
.addMigrations(MIGRATION_2_3)
.build();


nightclubdao = database.getNightClubDAO(); // from AppDatabase.java

Log.d(TAG, "here are they: " + database.getNightClubDAO());


nightclub_list = nightclubdao.getAllNightClubs(); // get all night clubs as a list


Log.d(TAG, "here are they: " + nightclub_list);


}

谢谢!

最佳答案

经过多次尝试,问题出在数据库上。多次架构更改和不一致的迁移。我必须完全重写数据库并用数据填充它。

关于java - 房间数据库中的列表数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59796192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com