我是Firebase的新手,我正在阅读教程,并练习Firebase连接。我想在我的Firebase中添加数据。但不知怎的,它没有添加。我不知道问题是什么。 Firebase已成功连接。 这是我的主要活动。
package com.example.sayan.appfire;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.firebase.client.Firebase;
public class MainActivity extends AppCompatActivity {
private Button msendData;
private Firebase mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
mRef = new Firebase("https://appfire-713c6.firebaseio.com/");
msendData = (Button)findViewById(R.id.sendData);
msendData.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Firebase mRefChild = mRef.child("name");
mRefChild.setValue("Sayan");
}
});
}
}
答案 0 :(得分:0)
尝试这种方式在firebase数据库上执行操作
private void createUser(String name, String email) {
// TODO
// In real apps this userId should be fetched
// by implementing firebase auth
if (TextUtils.isEmpty(userId)) {
userId = mFirebaseDatabase.push().getKey();
}
User user = new User(name, email);
mFirebaseDatabase.child(userId).setValue(user);
addUserChangeListener();
}
有关详情,请查看:
https://www.androidhive.info/2016/10/android-working-with-firebase-realtime-database/
答案 1 :(得分:0)
DatabaseReference mDatabase =
FirebaseDatabase.getInstance().getReference();
mDatabase.child("tasks").push().setValue(firebaseData, new
DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference
databaseReference) {
//Problem with saving the data
if (databaseError != null) {
} else {
//Data uploaded successfully on the server
}
}
});