数据不会发送到数据库。我需要将蛇名和学名发送到云中。我正在使用android studio,此代码中没有编译错误,但实时数据库未更新。
这是我的代码:-
MainActivity.java
$res = get_res('select * from my_table');
$descendants = get_descendants(2);
function get_descendants ($parent_id, $res) {
foreach ($res as $row) {
if ($row['parent_id'] == $parent_id) {
$descendants[] = $row['id'];
$descendants = $descendants + get_descendants($row['id'], $res);
}
}
return $descendants;
}
和我的蛇班:-
public class MainActivity extends AppCompatActivity {
EditText Snakename;
Button upload;
Spinner SciName;
DatabaseReference databaseSnake;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Snakename=(EditText)findViewById(R.id.snakeName);
upload=(Button)findViewById(R.id.upload);
SciName=(Spinner)findViewById(R.id.scientificName);
databaseSnake= FirebaseDatabase.getInstance().getReference("snake");
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addSnake();
}
});
}
private void addSnake(){
String snakename=Snakename.getText().toString().trim();
String SCINAME=SciName.getSelectedItem().toString();
if(!TextUtils.isEmpty(snakename)){
String id=databaseSnake.push().getKey();
Snake snake=new Snake(id,snakename,SCINAME);
databaseSnake.child(id).setValue(snake);
Toast.makeText(this,"Snake Added",Toast.LENGTH_LONG).show();;
}
else{
Toast.makeText(this,"You Should enter a name",Toast.LENGTH_LONG).show();
}
}
}
如何在运行时检查数据是否保存在数据库中?
答案 0 :(得分:0)
如果您想知道何时提交数据,可以添加完成监听器。 setValue()和updateChildren()都带有一个可选的完成侦听器,当写入已提交给数据库时,将调用该侦听器。如果由于某种原因呼叫失败,则将向侦听器传递一个错误对象,指示失败的原因:
databaseSnake.child(id).setValue(snake, new Firebase.CompletionListener() {
@Override
public void onComplete(FirebaseError firebaseError, Firebase firebase) {
if (firebaseError != null) {
Toast.makeText(this,"Data could not be saved. " +
firebaseError.getMessage(),Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this,""Data saved successfully.",Toast.LENGTH_LONG).show();
}
}
});
答案 1 :(得分:0)
尝试一下
if(!TextUtils.isEmpty(snakename)){
String id=databaseSnake.push().getKey();
Snake snake=new Snake(id,snakename,SCINAME);
Map userInfo = new HashMap();
userInfo.put(id, snake);
databaseSnake.updateChildren(userInfo);
Toast.makeText(this,"Snake Added",Toast.LENGTH_LONG).show();;
}
希望它能提供全部帮助。