更新android中的edittext

时间:2012-12-02 17:25:06

标签: android android-edittext

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

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();
            }
        });

1 个答案:

答案 0 :(得分:0)

代码的问题在于,即使在更新之前,您也要从EditTexts中获取值。您需要等待用户按下更新按钮,然后从EditTexts获取文本并将其传递给数据库进行保存。

请尝试以下代码。

    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();
            }
        });