我试图通过TextUtils.isEmpty验证两个字符串,但是每次都失败。
以下是我的代码:
private void addArtist() {
//getting the values to save
String email = editTextName.getText().toString().trim();
String mobileno = editTextName1.getText().toString().trim();
//checking if the value is provided
if (TextUtils.isEmpty(email)){
//if the value is not given displaying a toast
Toast.makeText(this, "Please enter email.", Toast.LENGTH_LONG).show();
}
else{
//getting a unique id using push().getKey() method
//it will create a unique id and we will use it as the Primary Key for our Artist
String id = databaseArtists.push().getKey();
//creating an Artist Object
Artist artist = new Artist(id, email, mobileno);
//Saving the Artist
databaseArtists.child(id).setValue(artist);
Toast.makeText(this, "Successful...", Toast.LENGTH_LONG).show();
}
}
当前此代码正在字符串电子邮件上运行,我希望它也可以与字符串mobileno一起使用。感谢您的帮助。
答案 0 :(得分:0)
如果“ Mobileno”变量应为int类型,为什么不尝试使用try catch块,并在其中尝试从TextField控件对象读取该字符串后将其转换为数字呢?如果失败,则可以在catch块中进行一些其他说明,以防止将其插入到特定对象的字段中。你尝试过吗?