Android 在微调器中使用LiveData

Android 在微调器中使用LiveData,android,mvvm,android-room,android-livedata,Android,Mvvm,Android Room,Android Livedata,我是安卓的新手。我在项目中使用room数据库、AndroidViewModel和LiveDate。在我的项目中有一个模型“收据”,我有一个名为“AddReceiptActivity”的活动,在这个活动中有一个微调器来显示和选择地点。还有另一个存储场所的场所模型。如何在“选择”微调器中显示放置表格(模型)的值。 这是我的地方 @Entity public class Place implements Serializable { @PrimaryKey(autoGenerate = true) p

我是安卓的新手。我在项目中使用room数据库、AndroidViewModel和LiveDate。在我的项目中有一个模型“收据”,我有一个名为“AddReceiptActivity”的活动,在这个活动中有一个微调器来显示和选择地点。还有另一个存储场所的场所模型。如何在“选择”微调器中显示放置表格(模型)的值。 这是我的地方

@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;
    }
}
}

我假设你有一个列表,它是存储位置列表

假设
List places=new ArrayList()

首先,您应该为Place model使用ovverride-toString方法,以便在微调器中输入每个位置的名称

@ovverride
public String toString(){
 return this.placeName;
}
在活动类中,使用此函数将列表放入微调器中

public void setList(){
ArrayAdapter<Place> adapter =
                new ArrayAdapter<Place>(getApplicationContext(),  android.R.layout.simple_spinner_dropdown_item, places);
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
}

我假设你有一个列表,它是存储位置列表

假设
List places=new ArrayList()

首先,您应该为Place model使用ovverride-toString方法,以便在微调器中输入每个位置的名称

@ovverride
public String toString(){
 return this.placeName;
}
在活动类中,使用此函数将列表放入微调器中

public void setList(){
ArrayAdapter<Place> adapter =
                new ArrayAdapter<Place>(getApplicationContext(),  android.R.layout.simple_spinner_dropdown_item, places);
        adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(adapter);
}