我正在学习有关Android和ViewModel的知识,并且为了提高知识水平,我正在开发一个Android项目,目标是与新知识一起增加和重构它。在这一刻,我仅使用ViewModel,LiveData和Activity(无片段)。该项目是一本通讯录,在第一个屏幕中有一个recyclerview来显示联系人,在底部有一个Fab可转到第二个屏幕。在第二个屏幕中,有一些字段可用于创建新联系人。要保存联系人,请使用静态属性。在第一个屏幕中,我创建一个具有LiveData的ViewModel以更新RecyclerView,在第二个屏幕中,我创建一个ViewModel以获取数据并将其发送到资源库以静态保存联系人。我想知道当我将联系人保存在第二个屏幕中时如何更新实时数据,以便当您关闭它并返回到第一个屏幕时,将更新回收者视图。我可以注册该联系人,但是当我返回上一个屏幕时,不显示新值,如果这是一个愚蠢的问题,我深表歉意。这是我的代码:
AgendaListActivity(MainActivity)
class AgendaListActivity : AppCompatActivity() {
private lateinit var viewModel: AgendaListViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_agenda_list)
viewModel = ViewModelProviders.of(this).get(AgendaListViewModel::class.java)
val observer = Observer<List<Contact>> {
agenda_list.apply {
layoutManager = LinearLayoutManager(this@AgendaListActivity, RecyclerView.VERTICAL, false)
setHasFixedSize(true)
adapter = AgendaListAdapter(it)
}
}
viewModel.contactList.observe(this, observer)
activity_agenda_list_fabAddContact.setOnClickListener {
onClickAddContact()
}
viewModel.getContacts()
}
override fun onResume() {
super.onResume()
}
fun onClickAddContact() {
startActivity(Intent(this, ContactFormActivity::class.java))
}
}
AgendaListViewModel
class AgendaListViewModel: ViewModel() {
var contactList: MutableLiveData<List<Contact>> = MutableLiveData()
var contactRepository:ContactRepository = ContactRepository()
fun getContacts() {
contactList.value = contactRepository.getContactList()
}
}
ContactFormActivity
class ContactFormActivity : AppCompatActivity() {
private lateinit var name: String
private lateinit var phone: String
private lateinit var email: String
private lateinit var viewModel: ContactFormViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_contact_form)
viewModel = ViewModelProviders.of(this).get(ContactFormViewModel::class.java)
activity_contact_form_btnAddContact.setOnClickListener {
onClickAddContact()
}
}
fun onClickAddContact() {
getDataFromEditText()
setDataToViewModel()
viewModel.saveContact()
finish()
}
fun getDataFromEditText() {
name = activity_contact_form_editName.text.toString()
phone = activity_contact_form_editPhone.text.toString()
email = activity_contact_form_editEmail.text.toString()
}
fun setDataToViewModel() {
viewModel.setName(name)
viewModel.setPhone(phone)
viewModel.setEmail(email)
}
}
ContactFormViewModel
class ContactFormViewModel: ViewModel() {
private var name: String = ""
private var phone: String = ""
private var email: String = ""
private var contactRepository = ContactRepository()
private var contact = Contact()
fun saveContact() {
setContact()
contactRepository.addContact(contact)
}
private fun setContact() {
contact.setName(name)
contact.setPhone(phone)
contact.setEmail(email)
}
fun setName(name: String) {
this.name = name
}
fun setPhone(phone: String) {
this.phone = phone
}
fun setEmail(email: String) {
this.email = email
}
}
ContactRepository
class ContactRepository {
companion object {
var contactList: MutableList<Contact> = ArrayList<Contact>()
}
fun addContact(contact: Contact) {
contactList.add(contact)
}
fun getContactList(): MutableList<Contact> {
return ArrayList<Contact>(contactList)
}
}
联系人(模型)
class Contact {
private var name: String = ""
private var phone: String = ""
private var email: String = ""
fun setName(name: String) {
this.name = name
}
fun setPhone(phone: String) {
this.phone = phone
}
fun setEmail(email: String) {
this.email = email
}
fun getName(): String {
return name
}
}
答案 0 :(得分:0)
如果您想保存联系人并刷新回收者视图,则可以执行以下选项: