Я использую библиотеку постоянства Room для получения данных из моей базы данных, но она возвращает пустой массив ( nightclub_list ).
Просто добавив, что фрагмент реализует OnMapReadyCallback для карты Google и getMapAsyn c находится в onCreateView . Тем не менее, база данных находится в "onViewCreated".
Я полностью новичок в этом.
ENTITY
@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) {
}
};
}
MapFragment
@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);
}
Спасибо!