简体   繁体   English

如何在“更新”按钮上单击对象以更新存储在对象中的值-Android?

[英]How to update a value stored in an Object on “Update” button click - Android?

I have the following class for an android application. 我有以下针对android应用程序的类。

public class AccountVO
{

    private String id;
    private String username;
    private String password;
    private String email;
    private String firstName;
    private String lastName;

    public AccountVO(String tempID, String tempUserName, String tempPassword, String tempEmail, String tempFirstName, String tempLastName)
    {
        this.id = tempID;
        this.username = tempUserName;
        this.password = tempPassword;
        this.email = tempEmail;
        this.firstName = tempFirstName;
        this.lastName = tempLastName;
    }

    public String getId()
    {
        return id;
    }

    public String getUserName()
    {
        return username;
    }

    public String getEmail()
    {
        return email;
    }

    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }
}

I create the following objects in an activity. 我在活动中创建以下对象。

AccountVO userAccount = new AccountVO("1", "scott", "password", "scott@mail.com", "Scott", "James");
AccountVO userAccount2 = new AccountVO("2", "john", "password", "jsmith@mail.com", "John", "Smith");

I have another activity where I retrieve the values from the objects which I created above and display them in EditText fields. 我还有另一项活动,我从上面创建的对象中检索值并将其显示在EditText字段中。 Suppose I change the data in the fields and click on the "Update" Button, can anyone please tell as to how to update the values in my old AccountVO object? 假设我更改了字段中的数据,然后单击“更新”按钮,谁能告诉我如何更新旧AccountVO对象中的值? For Example: If I change the email via the edittitext field in "userAccount" AccountVO object (to say scott@abc.com), How to update that value in the same object? 例如:如果我通过“ userAccount” AccountVO对象(例如scott@abc.com)中的edittitext字段更改电子邮件,如何在同一对象中更新该值?

Write a setter for each of the fields you have a getter for, like so: 为您有一个getter的每个字段编写一个setter,如下所示:

public void setLastName(String lastName) {
    this.lastName = lastName;
}

Then, call the setter in the Update function: 然后,在Update函数中调用setter:

public void Update() { 
    userAccount.setLastName(editText.getText().toString());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM