问题
I have a editText and a button, if i type "abc" and hit the button it will store the value in my database and will open a dielog box saying it got insert successfully but in the dialog box i also want to show what got insert, so heres the code:
EditText texto = (EditText) findViewById(R.id.etAdd);
final String resultado = texto.getText().toString();
Button add = (Button) findViewById(R.id.bAdd);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
//Abre ou Cria uma database com nome de: "dbtest.db"
db = openOrCreateDatabase("dbtest.db", Context.MODE_PRIVATE, null);
try {
db.insert("usuarioorigem", resultado, null);
ShowMessage("Mensagens","Inserido" +resultado+ "com sucesso!");
} finally {
}
} finally {
}
}
});
回答1:
The key is to check for the success of insert operation
EditText texto = (EditText) findViewById(R.id.etAdd);
Button add = (Button) findViewById(R.id.bAdd);
add.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try{
//Abre ou Cria uma database com nome de: "dbtest.db"
db = openOrCreateDatabase("dbtest.db", Context.MODE_PRIVATE, null);
String resultado = texto.getText().toString();
try{
long result = db.insert("usuarioorigem", resultado, null);
if(result == -1){
ShowMessage("Error in Insert"); //sorry for english here, may be u can use inserido
}
else{
ShowMessage("Mensagens Inserido" +resultado+ "com sucesso!");
}
}finally{
}
}finally{
}
}
});
private void ShowMessage(String msg){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
回答2:
Your ShowMessage function could look like this:
private void ShowMessage(String msg){
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
See here for more info on Toasts.
来源:https://stackoverflow.com/questions/12288038/how-to-insert-a-string-value-into-a-dialog-message