1

是否可以在已设置的 edittext 中更新文本?我已经用我的列表视图中的一个人的姓名和电话号码填写了我的编辑文本,我希望用户编辑这些详细信息。当我尝试输入例如姓氏并单击更新按钮时,它无法保存我的输入,但仍然保存了我从列表视图中传递的那个。

EditText sqlName = (EditText)findViewById(R.id.editName);
    EditText sqlNumber = (EditText)findViewById(R.id.editNumber);
    name = CustomListView.name;
    num = CustomListView.number;
    Button bUpdate = (Button)findViewById(R.id.editUpdate);
    Button bView = (Button)findViewById(R.id.editView);
    sqlName.setText(name);
    sqlNumber.setText(num);
    nameChanged = sqlName.getText().toString();
    numChanged = sqlNumber.getText().toString();
    bUpdate.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            GroupDb info = new GroupDb(EditDetails.this);
            info.open();
            long rowid = info.getRowId(name, num);
            Toast.makeText(getApplicationContext(), nameChanged+" "+numChanged+" ", Toast.LENGTH_LONG).show();
            info.updateNameNumber(rowid, nameChanged, numChanged);
            Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show();
            info.close();
            }
        });
4

1 回答 1

0

The problem with your code is that you are fetching the values from the EditTexts even before they were updated. You need to wait for the user to press the update button and then get the text from the EditTexts and pass them to the database to save.

Try the following code.

    EditText sqlName = (EditText)findViewById(R.id.editName);
    EditText sqlNumber = (EditText)findViewById(R.id.editNumber);
    name = CustomListView.name;
    num = CustomListView.number;
    Button bUpdate = (Button)findViewById(R.id.editUpdate);
    Button bView = (Button)findViewById(R.id.editView);
    sqlName.setText(name);
    sqlNumber.setText(num);
    bUpdate.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            nameChanged = sqlName.getText().toString();
            numChanged = sqlNumber.getText().toString();
            GroupDb info = new GroupDb(EditDetails.this);
            info.open();
            long rowid = info.getRowId(name, num);
            Toast.makeText(getApplicationContext(), nameChanged+" "+numChanged+" ", Toast.LENGTH_LONG).show();
            info.updateNameNumber(rowid, nameChanged, numChanged);
            Toast.makeText(getApplicationContext(), "Update Successful!", Toast.LENGTH_LONG).show();
            info.close();
            }
        });
于 2012-12-02T17:29:13.773 回答