How to insert a string value into a dialog message?

白昼怎懂夜的黑 提交于 2019-12-08 07:59:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!