我正在使用Room数据库和实时数据。重建项目时,出现错误。我收到以下代码的错误:
@Query("SELECT * FROM book_table")
LiveData<List<Book>> getBooks();
错误日志:
error: Not sure how to convert a Cursor to this method's return type (LiveData<List<Book>>)
Room.java
@Entity(tableName = "book_table", foreignKeys = @ForeignKey(entity = Category.class, parentColumns = "id", childColumns = "category_id", onDelete = CASCADE))
public class Book extends BaseObservable {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "book_name")
private String name;
@ColumnInfo(name = "book_price")
private double price;
@ColumnInfo(name = "category_id")
private int categoryId;
public Book(String name, double price, int categoryId) {
setName(name);
setPrice(price);
setCategoryId(categoryId);
}
@Bindable
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
notifyPropertyChanged(BR.id);
}
@Bindable
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
notifyPropertyChanged(BR.name);
}
@Bindable
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
notifyPropertyChanged(BR.price);
}
@Bindable
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
notifyPropertyChanged(BR.categoryId);
}
}
在此也使用数据绑定。该项目中有两个表。类别和书籍。