How to insert a string value into a dialog message

2019-06-07 18:14发布

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 {

        }
    }
});

2条回答
别忘想泡老子
2楼-- · 2019-06-07 19:13

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.

查看更多
神经病院院长
3楼-- · 2019-06-07 19:18

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();
    }
查看更多
登录 后发表回答