【发布时间】:2020-01-28 08:30:00
【问题描述】:
我是安卓新手。我在我的项目中使用房间数据库、AndroidViewModel 和 LiveDate。我的项目中有一个模型“Receipt”,我有一个名为“AddReceiptActivity”的活动,该活动中有一个 Spinner 可以显示并选择 Place。 Place 还有另一种存储地点的模型。如何在 Spinner 中显示放置表的(模型)值以供选择。 这是我的地方实体
@Entity
public class Place implements Serializable {
@PrimaryKey(autoGenerate = true)
private Integer placeId;
private String placeName;
public Place(String placeName, Boolean removed) {
this.placeName = placeName;
this.removed = removed;
}
public Integer getPlaceId() {
return placeId;
}
public void setPlaceId(Integer placeId) {
this.placeId = placeId;
}
public String getPlaceName() {
return placeName;
}
public void setPlaceName(String placeName) {
this.placeName = placeName;
}
}
这是我的 ViewModel
public class AddReceiptViewModel extends AndroidViewModel {
DataBase database;
public AddReceiptViewModel(@NonNull Application application) {
super(application);
database = DataBase.getDataBase(this.getApplication());
}
public void addRceipt(final Receipt receipt) {
new AddAsyncTask(database).execute(receipt);
}
private static class AddAsyncTask extends AsyncTask<Receipt, Void, Void> {
DataBase db;
public AddAsyncTask(DataBase db) {
this.db = db;
}
@Override
protected Void doInBackground(Receipt... receipts) {
db.receiptDao().add(receipts[0]);
return null;
}
}
}
现在我不知道如何在 Activity 中将数据从我的模型设置到 Spinner。
AddReceiptViewModel addReceiptViewModel = ViewModelProviders.of(this).get(AddReceiptViewModel.class);
Spinner spinner = findViewById(R.id.spinnerLoadingPlace);
【问题讨论】:
标签: android mvvm android-room android-livedata