2012-12-02 85 views
1

是否有可能在edittext中更新已設置的文本?我已經用我的列表視圖中的姓名和電話號碼填充了我的edittext,並且我希望用戶編輯這些詳細信息。當我試圖輸入例如姓氏並單擊更新按鈕時,它不能保存我的輸入並仍保存我從我的列表視圖傳來的信息。更新android中的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(); 
      } 
     }); 

回答

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