gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:21 24 4
gpt4 key购买 nike

我的问题是,在将写入的内容保存到数据库后,如何清除 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

}
}

最佳答案

您需要调用:

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

关于android - 将信息保存到数据库后清除 editText 输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9819582/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com