gpt4 book ai didi

android - 测试字段是否为空并阻止用户保存到 sqlite - android

转载 作者:行者123 更新时间:2023-11-29 16:10:36 24 4
gpt4 key购买 nike

我如何测试该字段是否由用户填写?我想提示他们必须完全填写字段。但是即使名称为空并且输入了数字,此代码仍然接受。

public void onClick(View arg0)
{
switch(arg0.getId())
{
case R.id.bSQLUpdate:
boolean diditwork = true;
try
{
String name = sqlName.getText().toString();
String number = sqlNumber.getText().toString();

GroupDb entry = new GroupDb(ContactDb.this);
entry.open();
if(name != null && number != null)
{
if((!entry.hasDuplicateNameNumber(name, number)) )
{
entry.createEntry(name, number);

}
else
{
diditwork = false;
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText("Duplicate name or number. Please try again.");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
}
else
{
diditwork = false;
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText("Please fill up all fields. Please try again.");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}

entry.close();
}
catch(Exception e)
{
diditwork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Update failed");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
finally
{
if(diditwork)
{
Dialog d = new Dialog(this);
d.setTitle("Contact list updated");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
sqlName.setText("");
sqlNumber.setText("");
}
}
break;

case R.id.bSQLOpenView:
Intent intent = new Intent();
intent.setClass(ContactDb.this, CustomListView.class);

startActivityForResult(intent, 0);
break;

}

}// end onclick

最佳答案

尝试检查以下条件

if((name.length() > 0) && (number .length() > 0))
{
// do the work

}

代替

if(name != null && number != null)
{
// do the work
}

关于android - 测试字段是否为空并阻止用户保存到 sqlite - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736313/

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