此应用程序创建贺卡。流程是在片段#1上选择照片,在片段#2上选择联系人,在片段#3上输入消息,在片段#4上查看、确认并发送。
此应用程序的数据存储在SQLite中(使用Room)。
当前状态是使用bundle/arguments的组合在片段之间共享数据,并保存到SQLite。太乱了
未来状态是将这些片段的所有数据放入现有的ViewModel中,以便所有片段中的所有数据都可用(然后在完成卡后保存到SQLite)。
我想在ViewModel中使用SQLite结构所基于的同一个类--基本上,只要调用ViewModel,就示例化它。这是一个类:
class DBClasses {
@Entity(tableName="photos")
data class photos(
@ColumnInfo(name = "user_id")
val user_id: String,
@PrimaryKey(autoGenerate = true)
val photo_id: Int,
@ColumnInfo(name = "photo_name")
val photo_name: String,
@ColumnInfo(name = "upload_date")
val upload_date: String,
@ColumnInfo(name = "status")
val status: String
)
@Entity(tableName="cards")
data class postcards(
@ColumnInfo(name = "user_id")
val user_id: String,
@PrimaryKey(autoGenerate = true)
val postcard_id: Int,
@ColumnInfo(name = "photo_id")
val photo_id: Int,
@ColumnInfo(name = "status")
val status: String,
@ColumnInfo(name = "message_id")
val message_id: Int,
@ColumnInfo(name = "contact_id")
val contact_id: Int
)
@Entity(tableName="contacts")
data class contacts(
@ColumnInfo(name = "user_id")
val user_id: String,
@PrimaryKey(autoGenerate = true)
val contact_id: Int,
@ColumnInfo(name = "status")
val status: String,
@ColumnInfo(name = "addressee")
val addressee: String,
@ColumnInfo(name = "first_name")
var first_name: String,
@ColumnInfo(name = "last_name")
val last_name: String,
@ColumnInfo(name = "address")
var address: String,
@ColumnInfo(name = "city")
val city: String,
@ColumnInfo(name = "state")
val state: String,
@ColumnInfo(name = "zip_code")
val zip_code: String
)
@Entity(tableName="messages")
data class messages(
@ColumnInfo(name = "user_id")
val user_id: String,
@PrimaryKey(autoGenerate = true)
val message_id: Long,
@ColumnInfo(name = "status")
val status: String,
@ColumnInfo(name = "message")
val message: String,
@ColumnInfo(name = "string")
val string: String,
)
@Entity(tableName = "owner")
data class owner(
@PrimaryKey(autoGenerate = true)
val owner_id: Long,
@ColumnInfo(name = "user_id")
val user_id: String,
@ColumnInfo(name = "status")
val status: String,
@ColumnInfo(name = "sender")
val sender: String,
@ColumnInfo(name = "first_name")
val first_name: String,
@ColumnInfo(name = "last_name")
val last_name: String,
@ColumnInfo(name = "address")
val address: String,
@ColumnInfo(name = "city")
val city: String,
@ColumnInfo(name = "state")
val state: String,
@ColumnInfo(name = "zip_code")
val zip_code: String
)
}
有办法做到这一点吗?否则,我认为我必须在ViewModel中重新创建类,这似乎是不必要的重复。我尝试在ViewModel中示例化现有的类(DBClasses),这允许我这样做,但是当我尝试实际引用片段中的空值时,我会遇到错误。
编辑:为了访问值,我采取了另一种方法,在ViewModel中设置它-请参阅下面的联系人示例:
private val _user_id = MutableLiveData<String>("")
val user_id: LiveData<String> = _user_id
private val _contact_id = MutableLiveData<Int>(0)
val contact_id: LiveData<Int> = _contact_id
private val _status = MutableLiveData<String>("")
val status: LiveData<String> = _status
private val _addressee = MutableLiveData<String>("")
val addressee: LiveData<String> = _addressee
private val _first_name = MutableLiveData<String>("")
val first_name: LiveData<String> = _first_name
private val _last_name = MutableLiveData<String>("")
val last_name: LiveData<String> = _last_name
private val _address = MutableLiveData<String>("")
val address: LiveData<String> = _address
private val _city = MutableLiveData<String>("")
val city: LiveData<String> = _city
private val _state = MutableLiveData<String>("")
val state: LiveData<String> = _state
private val _zip_code = MutableLiveData<String>("")
val zip_code: LiveData<String> = _zip_code
fun setUserID(user_id: String) {
_user_id.value = user_id
}
fun setStatus(status: String) {
_status.value = status
}
fun setAddressee(addressee: String) {
_addressee.value = addressee
}
fun setFirstName(first_name: String) {
_first_name.value = first_name
}
fun setLastName(last_name: String) {
_last_name.value = last_name
}
fun setAddress(address: String) {
_address.value = address
}
fun setCity(city: String) {
_city.value = city
}
fun setState(state: String) {
_state.value = state
}
fun setZip(zip_code: String) {
_zip_code.value = zip_code
}
1条答案
按热度按时间dfty9e191#
通过使用
by activityViewModels()
初始化视图模型,可以在活动级别中仅使用一个视图模型。这个视图模型将保持现有状态,即使您的片段正在改变。如果您觉得您的片段中有一个复杂的状态管理,您也可以将它与每个片段视图模型结合起来。