将信息保存到数据库后清除editText输入

时间:2012-03-22 09:35:59

标签: android database sqlite android-edittext

我的问题是,在将已写入的内容保存到数据库后,如何清除editText字段?我目前可以使用nameEditText字段输入文本,但是当我单击InsertButton时,它不会清除for。我只想清除表单而不是数据库中的值或字符串...这是我想要用作清除方法的插入按钮:

  class InsertButtonListener implements OnClickListener, android.view.View.OnClickListener
    {
        public void onClick(View v) 
        {

            if("".equals(nameEditText.getText().toString())) 
            {
                Toast toast = Toast.makeText(Entername.this, "Sorry, you must input both the name and the address!", Toast.LENGTH_LONG); 
                toast.show();
            }
            else
            {
                long flag = 0;
                int id = 1;
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("user_name", new String[]{"count(*) ID"}, null, null, null, null, null);
                while(cursor.moveToNext())
                {
                    int idFromDatabase = cursor.getInt(cursor.getColumnIndex("ID"));
                    if(idFromDatabase != 0)
                    {
                        id = 1 + idFromDatabase;
                    }
                }
                ContentValues values = new ContentValues();
                values.put("ID", id);
                values.put("name", nameEditText.getText().toString().trim());
                //values.put("address", addressEditText.getText().toString().trim());
                flag = db.insert("user_name", null, values);
                if(flag != -1)
                {
                    Toast toast = Toast.makeText(Entername.this, "You have successful inserted this record into database! ", Toast.LENGTH_LONG); 
                    toast.show();
                    db.close();
                    return;
                }
                else
                {
                    Toast toast = Toast.makeText(Entername.this, "An error occured when insert this record into database!", Toast.LENGTH_LONG); 
                    toast.show();
                    db.close();
                    return;
                }
            }
        }

        public void onClick(DialogInterface dialog, int which) 
        {
            // TODO Auto-generated method stub

        }
    }

2 个答案:

答案 0 :(得分:4)

您需要致电:

nameEditText.setText("");
addressEditText.setText("");

因此,在添加到数据库成功时,在 if 条件中执行以下更改:

if(flag != -1)
{
    Toast toast = Toast.makeText(Entername.this,
        "You have successful inserted this record into database! ",
        Toast.LENGTH_LONG); 
   toast.show();
   db.close();

   //clearing edittexts
   nameEditText.setText("");
   addressEditText.setText("");

   return;
}

答案 1 :(得分:0)

你在editText之后最后插入以设置空值

EditText text;  

 text.setText("");