在firebase android中保存配置文件信息

时间:2016-09-19 13:50:16

标签: android firebase firebase-realtime-database firebase-authentication

这是我的代码,我用它来保存firebase数据库中的一些EditText字段。 谁能告诉我我做错了什么? 提前致谢

private FirebaseAuth.AuthStateListener authListener;
private FirebaseAuth auth;

private Button buttonSave;

private Firebase mref;

private EditText editTextName;
private EditText editDOB;
private EditText editphone;
private EditText editcity;
private EditText editclass;
private EditText editboard;
private EditText editschool;
private EditText edithobbies;
private EditText editachievements;
FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener mAuthListener;

static final String TAG = "YOUR-TAG-NAME";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    Firebase.setAndroidContext(this);



    editTextName = (EditText) findViewById(R.id.editTextName);
    editDOB = (EditText) findViewById(R.id.editDOB);
    editphone = (EditText) findViewById(R.id.editphone);
    editcity = (EditText) findViewById(R.id.editcity);
    editclass = (EditText) findViewById(R.id.editclass);
    editboard = (EditText) findViewById(R.id.editboard);
    editschool = (EditText) findViewById(R.id.editschool);
    edithobbies = (EditText) findViewById(R.id.edithobbies);
    editachievements = (EditText) findViewById(R.id.editachievements);

    mref = new Firebase("http://***********.firebaseio.com/Users");

    mAuthListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if (user != null) {
                // User is signed in
                Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
            } else {
                // User is signed out
                Log.d(TAG, "onAuthStateChanged:signed_out");
            }
            // ...
        }
    };

    FirebaseUser userID = FirebaseAuth.getInstance().getCurrentUser();
    if (userID != null) {
        buttonSave = (Button) findViewById(R.id.buttonSave);
        System.out.println("userID:"+ userID);
        buttonSave.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick (View view){
                String name = editTextName.getText().toString();
                Firebase Childref1 = mref.child("Name");
                System.out.println("nameis:"+ name);
                Childref1.setValue(name , new Firebase.CompletionListener() {
                    @Override
                    public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                        System.out.println("completedcall");
                        if (firebaseError != null) {
                            System.out.println("DataNotSaved" + firebaseError.getMessage());
                        } else {
                            System.out.println("DataSaved");
                        }
                    }
                });
.
.
.
.
//similarily for other fields

尝试功能仅执行此功能

public void onComplete(FirebaseError firebaseError, Firebase firebase) {
                        System.out.println("completedcall");
}

未打印完成电话

1 个答案:

答案 0 :(得分:1)

您只需按照以下链接,我认为它会对您有所帮助。

https://www.simplifiedcoding.net/android-firebase-tutorial-1/?_e_pi_=7%2CPAGE_ID10%2C4980758429