如何使用Android在Firebase数据库中存储多值属性

时间:2016-10-15 11:12:54

标签: android firebase firebase-realtime-database

我正在制作一个让人们分享技能的机器人。如何将他们的技能存储在火力库中,因为他们每个人都拥有不同数量的技能。 我所采取的属性是姓名,电子邮件,手机号码,技能。 当我更新技能属性时,旧值将被新值替换。但是我希望在不丢失旧值的情况下添加新值。

When save button is clicked then name and number must be saved to firebase and if add skill is clicked then skills must be added to the firebase

这是截图Activity的代码。如果可能,我可以在一个json中获得所有这些属性吗?

package com.coginitoamicis.coginitoamicis;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Account extends AppCompatActivity implements View.OnClickListener {

private EditText accname;
private EditText accnumb;
private EditText accskill;

private Button accsave;
private Button accskilladd;

private DatabaseReference databaseReference;
private FirebaseAuth firebaseAuth;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_account);

    firebaseAuth = FirebaseAuth.getInstance();

    if (firebaseAuth.getCurrentUser()==null){
        finish();
        startActivity(new Intent(getApplicationContext(), LoginActivity.class));
    }

    databaseReference = FirebaseDatabase.getInstance().getReference();

    accname = (EditText) findViewById(R.id.accname);
    accnumb = (EditText) findViewById(R.id.accnumb);
    accsave = (Button) findViewById(R.id.accsave);

    accskill = (EditText) findViewById(R.id.accskill);
    accskilladd = (Button) findViewById(R.id.accskilladd);

    accsave.setOnClickListener(this);
    accskilladd.setOnClickListener(this);
}

private void userdatasave(){

    String name = accname.getText().toString().trim();
    String numb = accnumb.getText().toString().trim();

    if(TextUtils.isEmpty(name)){
        //name is empty
        Toast.makeText(this,"Please Enter your Name",Toast.LENGTH_SHORT).show();
        return;
    }

    if(TextUtils.isEmpty(numb)){
        //number is empty
        Toast.makeText(this,"Please Enter your Mobile Number",Toast.LENGTH_SHORT).show();
        return;
    }

    UserInformation userInformation = new UserInformation(name, numb);

    FirebaseUser user =firebaseAuth.getCurrentUser();

    databaseReference.child(user.getUid()).setValue(userInformation);

    Toast.makeText(this,"Information Saved",Toast.LENGTH_SHORT).show();

}

private void skilldatasave(){

    String skill = accskill.getText().toString().trim();

    if(TextUtils.isEmpty(skill)){
        //skills is empty
        Toast.makeText(this,"Enter Skill to Proceed",Toast.LENGTH_SHORT).show();
        return;
    }

    Skilladd skilladds = new Skilladd(skill);

    FirebaseUser user =firebaseAuth.getCurrentUser();

    databaseReference.child(user.getUid()).setValue(skilladds);

    Toast.makeText(this,"Skills Added Successfully",Toast.LENGTH_SHORT).show();

}

@Override
public void onClick(View view) {

    if (view == accsave){
        userdatasave();
    }

    if (view == accskilladd){
        skilldatasave();
    }

}
}

0 个答案:

没有答案