如何在Android中访问返回的意图中的额外数据并更新TextView

如何在Android中访问返回的意图中的额外数据并更新TextView,android,kotlin,android-intent,Android,Kotlin,Android Intent,我正在尝试更新第一个活动中的值。为了这个。我已经将参数从第一个活动传递到第二个活动,并使用intent和update文本输入。现在,我想将更新后的值传递给第一个活动,并更新特定的值。我尝试使用setResult()方法在saveButton onClick中发送数据。并使用onActivityResult()方法在FirstActivity中获取值。但它不会更新值。你能查一下我哪里做错了吗 第一项活动 class ShowProfileActivity : AppCompatActivity()

我正在尝试更新第一个活动中的值。为了这个。我已经将参数从第一个活动传递到第二个活动,并使用intent和update文本输入。现在,我想将更新后的值传递给第一个活动,并更新特定的值。我尝试使用setResult()方法在saveButton onClick中发送数据。并使用onActivityResult()方法在FirstActivity中获取值。但它不会更新值。你能查一下我哪里做错了吗

第一项活动

class ShowProfileActivity : AppCompatActivity() {

    private lateinit var fullnameTv: TextView
    private lateinit var nicknameTv: TextView
    private lateinit var locationTv: TextView
    private lateinit var emailTv: TextView
    private lateinit var bioTv: TextView

    // todo load from sp
    private val mockUser: User? = User("TestName", "test", "test@gmail.com", "Italy", "this is a mocked bio")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_show_profile)

        fullnameTv = findViewById(R.id.asp_user_name_tv)
        nicknameTv = findViewById(R.id.asp_user_nickname_tv)
        locationTv = findViewById(R.id.asp_user_location_tv)
        emailTv = findViewById(R.id.asp_user_email_tv)
        bioTv = findViewById(R.id.asp_user_bio_tv)


        // init views with user data
        fullnameTv.text = mockUser?.fullname
        nicknameTv.text = mockUser?.nickname
        locationTv.text = mockUser?.location
        emailTv.text = mockUser?.email
        bioTv.text = mockUser?.bio




        var editButton = findViewById<ImageButton>(R.id.editBtn);
        editButton.setOnClickListener {
            editProfile();
        }
    }

    fun editProfile() {
        val intent = Intent(this@ShowProfileActivity,EditProfileActivity::class.java)
        if (mockUser != null) {
            intent.putExtra("group23.lab1.user_fullname", mockUser.fullname)
            intent.putExtra("group23.lab1.user_nickname", mockUser.nickname)
            intent.putExtra("group23.lab1.user_location", mockUser.location)
            intent.putExtra("group23.lab1.user_email", mockUser.email)
            intent.putExtra("group23.lab1.user_bio", mockUser.bio)
        }
        startActivityForResult(intent, 1)
        startActivity(intent)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == Activity.RESULT_OK){
            if (data != null) {
                if (mockUser != null) {
                    mockUser.fullname= data.getStringExtra("group23.lab1.user_fullname").toString() //not updated
                }
            }
        }

    }





    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.show_profile_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.menu_show_profile_edit -> {
                /*
                TODO start edit profile activity (check activity name)
                val intent = Intent(this, com.group23.lab1.EditProfileActivity::class.java).apply {
                    if (mockUser != null) {
                        putExtra("group23.lab1.user_fullname", mockUser.fullname)
                        putExtra("group23.lab1.user_nickname", mockUser.nickname)
                        putExtra("group23.lab1.user_location", mockUser.location)
                        putExtra("group23.lab1.user_email", mockUser.email)
                        putExtra("group23.lab1.user_bio", mockUser.bio)
                    }
                }
                startActivityForResult(intent)
                */
                Toast.makeText(this, "OPEN EDIT PROFILE", Toast.LENGTH_SHORT).show()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}
类ShowProfileActivity:AppCompatActivity(){
私有lateinit var fullnameTv:TextView
私有lateinit变量昵称TV:TextView
私有lateinit var locationTv:TextView
私有lateinit var emailTv:TextView
私有lateinit var bioTv:TextView
//从sp加载待办事项
private val mockUser:User?=用户(“TestName”、“test”、”test@gmail.com“,”意大利“,”这是一篇嘲弄的传记“)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u show\u profile)
fullnameTv=findviewbyd(R.id.asp\u user\u name\u tv)
昵称tv=findViewById(R.id.asp\u用户\u昵称\u tv)
locationTv=findViewById(R.id.asp\u用户\u位置\u电视)
emailTv=findViewById(R.id.asp\u用户\u电子邮件\u电视)
bioTv=findviewbyd(R.id.asp\u user\u bio\u tv)
//包含用户数据的初始化视图
fullnameTv.text=mockUser?.fullname
昵称tv.text=mockUser?.昵称
locationTv.text=mockUser?.location
emailTv.text=mockUser?.email
bioTv.text=mockUser?.bio
var editButton=findviewbyd(R.id.editBtn);
editButton.setOnClickListener{
editProfile();
}
}
有趣的编辑配置文件(){
val intent=intent(this@ShowProfileActivity,EditProfileActivity::class.java)
if(mockUser!=null){
intent.putExtra(“group23.lab1.user\u fullname”,mockUser.fullname)
intent.putExtra(“group23.lab1.user\u昵称”,mockUser.昵称)
intent.putExtra(“group23.lab1.user\u位置”,mockUser.location)
intent.putExtra(“group23.lab1.user\u email”,mockUser.email)
intent.putExtra(“group23.lab1.user_bio”,mockUser.bio)
}
startActivityForResult(意向,1)
星触觉(意图)
}
重写activityResult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
if(resultCode==Activity.RESULT\u确定){
如果(数据!=null){
if(mockUser!=null){
mockUser.fullname=data.getStringExtra(“group23.lab1.user_fullname”).toString()//未更新
}
}
}
}
重写创建选项菜单(菜单:菜单?):布尔值{
菜单充气器。充气(右菜单。显示配置文件菜单,菜单)
返回真值
}
覆盖选项ItemSelected(项:菜单项):布尔值{
返回时间(item.itemId){
R.id.菜单\显示\配置文件\编辑->{
/*
TODO开始编辑配置文件活动(检查活动名称)
val intent=intent(这是com.group23.lab1.EditProfileActivity::class.java){
if(mockUser!=null){
putExtra(“group23.lab1.user\u fullname”,mockUser.fullname)
putExtra(“group23.lab1.user\u昵称”,mockUser.昵称)
putExtra(“group23.lab1.user\u位置”,mockUser.location)
putExtra(“group23.lab1.user_email”,mockUser.email)
putExtra(“group23.lab1.user_bio”,mockUser.bio)
}
}
startActivityForResult(意向)
*/
Toast.makeText(这是“打开编辑配置文件”,Toast.LENGTH\u SHORT.show())
真的
}
else->super.onOptionsItemSelected(项目)
}
}
}
第二项活动


class EditProfileActivity : AppCompatActivity() {

    private lateinit var fullnameTv: TextView
    private lateinit var nicknameTv: TextView
    private lateinit var locationTv: TextView
    private lateinit var emailTv: TextView
    private lateinit var bioTv: TextView


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_edit_profile)

        val intent = intent

        fullnameTv = findViewById(R.id.asp_user_name_tv)
        nicknameTv = findViewById(R.id.asp_user_nickname_tv)
        locationTv = findViewById(R.id.asp_user_location_tv)
        emailTv = findViewById(R.id.asp_user_email_tv)
        bioTv = findViewById(R.id.asp_user_bio_tv)
//        ageTv = findViewById(R.id.asp_user_age_tv)

        fullnameTv.text = intent.getStringExtra("group23.lab1.user_fullname")
        nicknameTv.text = intent.getStringExtra("group23.lab1.user_nickname")
        locationTv.text = intent.getStringExtra("group23.lab1.user_location")
        emailTv.text = intent.getStringExtra("group23.lab1.user_email")
        bioTv.text = intent.getStringExtra("group23.lab1.user_bio")



        var sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
        var editor = sharedPreferences.edit()

        var saveButton = findViewById<Button>(R.id.btnSave)

        val name = sharedPreferences.getString("group23.lab1.user_fullname", null)
        val nickName = sharedPreferences.getString("group23.lab1.user_nickname", null)
        val location = sharedPreferences.getString("group23.lab1.user_location", null)
        val email = sharedPreferences.getString("group23.lab1.user_emai", null)
        val bio = sharedPreferences.getString("group23.lab1.user_bio", null)

        fullnameTv.text = name
        nicknameTv.text = nickName
        locationTv.text = location
        emailTv.text = email
        bioTv.text = bio

        saveButton.setOnClickListener {

            val name = fullnameTv.text.toString()
            val nickName = nicknameTv.text.toString()
            val location = locationTv.text.toString()
            val email = emailTv.text.toString()
            val bio = bioTv.text.toString()
//            var age = ageTv.text.toString()

            editor.apply {
                putString("group23.lab1.user_fullname", name)
                putString("group23.lab1.user_nickname", nickName)
                putString("group23.lab1.user_location", location)
                putString("group23.lab1.user_email", email)
                putString("group23.lab1.user_bio", bio)
                apply();
                setResult(RESULT_OK, intent);
                finish();

            }
        }




    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.show_profile_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.menu_show_profile_edit -> {


                Toast.makeText(this, "OPEN EDIT PROFILE", Toast.LENGTH_SHORT).show()
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }

}



类EditProfileActivity:AppCompatActivity(){
私有lateinit var fullnameTv:TextView
私有lateinit变量昵称TV:TextView
私有lateinit var locationTv:TextView
私有lateinit var emailTv:TextView
私有lateinit var bioTv:TextView
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u edit\u profile)
val intent=intent
fullnameTv=findviewbyd(R.id.asp\u user\u name\u tv)
昵称tv=findViewById(R.id.asp\u用户\u昵称\u tv)
locationTv=findViewById(R.id.asp\u用户\u位置\u电视)
emailTv=findViewById(R.id.asp\u用户\u电子邮件\u电视)
bioTv=findviewbyd(R.id.asp\u user\u bio\u tv)
//ageTv=findViewById(R.id.asp\u用户\u年龄\u电视)
fullnameTv.text=intent.getStringExtra(“group23.lab1.user\u fullname”)
昵称tv.text=intent.getStringExtra(“group23.lab1.user\u昵称”)
locationTv.text=intent.getStringExtra(“group23.lab1.user\u位置”)
emailTv.text=intent.getStringExtra(“group23.lab1.user\u email”)
bioTv.text=intent.getStringExtra(“group23.lab1.user_bio”)
var sharedPreferences=getSharedPreferences(“SHARED\u PREF”,Context.MODE\u PRIVATE)
var editor=SharedReferences.edit()
var saveButton=findviewbyd(R.id.btnSave)
val name=SharedReferences.getString(“group23.lab1.user_fullname”,null)
val昵称=SharedReferences.getString(“group23.lab1.user_昵称”,null)
val location=SharedReferences.getString(“group23.lab1.user_location”,null)
val email=SharedReferences.getString(“group23.lab1.user_emai”,null)
val bio=SharedReferences.getString(“group23.lab1.user_bio”,null)
fullnameTv.text=名称
昵称tv.text=昵称
locationTv.text=位置
emailTv.text=电子邮件
比奥
// Return a NEW intent with NEW data, not this Activity's current intent
val resultIntent = Intent().putString("group23.lab1.user_fullname", name) 
setResult(RESULT_OK, resultIntent);
startActivityForResult(intent, 1)
startActivity(intent) // <- Delete this
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == Activity.RESULT_OK && requestCode == 1) { // Check for same request code
            if (data != null) {
                if (mockUser != null) {
                    mockUser.fullname= data.getStringExtra("group23.lab1.user_fullname").toString() //not updated
                }
            }
        }
    }
val intent = intent // Delete this - it's pointless
// Setting values here from Intent ...
fullnameTv.text = intent.getStringExtra("group23.lab1.user_fullname")
nicknameTv.text = intent.getStringExtra("group23.lab1.user_nickname")
locationTv.text = intent.getStringExtra("group23.lab1.user_location")
emailTv.text = intent.getStringExtra("group23.lab1.user_email")
bioTv.text = intent.getStringExtra("group23.lab1.user_bio")

var sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
var editor = sharedPreferences.edit()

var saveButton = findViewById<Button>(R.id.btnSave)

val name = sharedPreferences.getString("group23.lab1.user_fullname", null)
val nickName = sharedPreferences.getString("group23.lab1.user_nickname", null)
val location = sharedPreferences.getString("group23.lab1.user_location", null)
val email = sharedPreferences.getString("group23.lab1.user_emai", null)
val bio = sharedPreferences.getString("group23.lab1.user_bio", null)

// ... get completely overwritten here ... so setting them above is pointless
fullnameTv.text = name
nicknameTv.text = nickName
locationTv.text = location
emailTv.text = email
bioTv.text = bio
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
        if(resultCode == Activity.RESULT_OK && requestCode == 1) { // Check for same request code
            if (mockUser != null) {
                var sharedPreferences = getSharedPreferences("SHARED_PREF", Context.MODE_PRIVATE)
                mockUser.fullname= sharedPreferences.getString("group23.lab1.user_fullname", "")
            }
        }
    }