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

时间:2013-01-04 18:42:35

标签: java android

我有一个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;
    }
}

我在活动中创建了以下对象。

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

我有另一个活动,我从上面创建的对象中检索值并在EditText字段中显示它们。假设我更改字段中的数据并单击“更新”按钮,任何人都可以告诉我如何更新旧的AccountVO对象中的值?例如:如果我通过“userAccount”AccountVO对象(例如scott@abc.com)中的edittitext字段更改电子邮件,如何在同一对象中更新该值?

1 个答案:

答案 0 :(得分:1)

为你有getter的每个字段写一个setter,如下所示:

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

然后,在Update函数中调用setter:

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