首页 > 解决方案 > 我怎样才能将苛刻的地图传递给 Kotlin 上的另一个活动?

问题描述

我一直在使用 Firestore 开发数据库。数据库本身正在工作,但是当我尝试从harshMap 中获取值时,我得到一个空值,尽管我可以看到数据库(在页面上)显示了我想要获取的参数。

所以我的问题是下一个问题:是否有适当的方法从另一个活动中的harshMap 中获取值?

代码中有一些注释,请看一下。

活动“A”上的 setup()

private fun setup(){
           crearCuentaButton.setOnClickListener{
            if (inputEmail.text.isNotEmpty() && inputContraseña.text.isNotEmpty()){
                val db = FirebaseFirestore.getInstance()
                val userData = hashMapOf(
                    "username" to inputUsername.text.toString(),
                    "email" to inputEmail.text.toString()
                )
                db.collection("users")
                    .add(userData)
                FirebaseAuth.getInstance().createUserWithEmailAndPassword(inputEmail.text.toString(), inputContraseña.text.toString()) .addOnCompleteListener {
                    if(it.isSuccessful){
                        showAuth(it.result?.user?.email ?: "", ProviderType.Email)
                    }
                    else{
                        showError()
                    }
                }
                Intent(this, HomeActivity::class.java).also {
                    val userName = userData["username"]
                    println(userName)
                    it.putExtra("dataBaseUsernameValue", userName) //<------ this is the value I'm trying to get, the println shows the variable perfectly, but when passing it through, it doesn't.
                    
                }
            }
        }
}

在活动“B”上创建

class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_home)
    homeBanner()
    val username = intent.getStringExtra("dataBaseUsernameValue")

    println(username) //<----------------- here I get the "null"
}

标签: kotlin

解决方案


推荐阅读