gpt4 book ai didi

android - 如何在 MVVM 架构中使用来自 onActivityResult 的数据?

转载 作者:行者123 更新时间:2023-12-02 13:19:36 25 4
gpt4 key购买 nike

如何在MVVM架构中使用onActivityResult的数据?我需要向订阅从 ViewModel 接收数据的 RecyclerView 添加一个项目。如何将数据从 onActivityResult 发送到 ViewModel 类,然后更新我的列表?如果我立即从 onactivityresult 方法向适配器添加数据,则什么也不会发生。我在这个问题上需要任何帮助

Activity 类:

class ContactsActivity : AppCompatActivity(), ContactsListener {
private val TAG = "ContactsActivity"

@Inject
lateinit var factory: ViewModelProvider.Factory
lateinit var contactsViewModel: ContactsViewModel


companion object {
const val ADD_CONTACT_REQUEST = 200
}

private lateinit var mAdapter: ContactsAdapter

@SuppressLint("WrongConstant")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

toolbar_contacts.title = getString(R.string.contacts_toolbar_title)

add_new_contact_btn.setOnClickListener {
val intent = Intent(this@ContactsActivity, AddContactActivity::class.java)
startActivityForResult(intent, ADD_CONTACT_REQUEST)
}

DaggerContactsActivity_ContactsComponent.create().inject(this@ContactsActivity)
contactsViewModel = ViewModelProviders.of(this@ContactsActivity, factory).get(ContactsViewModel::class.java)
contactsViewModel.getContactsList().observe(this@ContactsActivity, Observer {

mAdapter = ContactsAdapter(this@ContactsActivity, it)
recycler_contacts.layoutManager =
LinearLayoutManager(applicationContext, OrientationHelper.VERTICAL, false)
recycler_contacts.adapter = mAdapter
recycler_contacts.setHasFixedSize(true)

mAdapter.sortByName()

})
}

// Dagger create
@Component (modules = [ContactsModule::class])
interface ContactsComponent {

fun inject (activity: ContactsActivity)
}

@Module
abstract class ContactsModule {

@Binds
abstract fun bindViewModelFactory(factory: ContactsViewModelFactory): ViewModelProvider.Factory
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == ADD_CONTACT_REQUEST && resultCode == RESULT_OK) {
val firstName: String? = data?.getStringExtra(AddContactActivity.EXTRA_FIRST_NAME)
val lastName: String? = data?.getStringExtra(AddContactActivity.EXTRA_LAST_NAME)
val phone: String? = data?.getStringExtra(AddContactActivity.EXTRA_PHONE)
val email: String? = data?.getStringExtra(AddContactActivity.EXTRA_EMAIL)
val notes: String? = data?.getStringExtra(AddContactActivity.EXTRA_NOTES)

val contacts = Contacts(firstName = firstName.toString(), lastName = lastName.toString(), phone = phone.toString(),
email = email.toString(), notes = notes.toString(), images = "")

contactsViewModel.get(contacts = contacts)
}
}

override fun setupContactsList(contactsList: ArrayList<Contacts>) {

mAdapter.setupContacts(contactsList = contactsList)
}
}

最佳答案

在 MVVM 架构中,您不需要使用 Activity 的 onActivityResult 来交换数据。相反,让目标 Activity 的 UI 绑定(bind)到 ViewModel 的 LiveData,后者进一步指向由另一个 Activity(通过其自己的 ViewModel)更新的相同数据源(例如 sqlite)。

关于android - 如何在 MVVM 架构中使用来自 onActivityResult 的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59632036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com