将数据从多个Android活动保存到一个Firebase数据库

时间:2018-07-17 07:30:23

标签: android firebase firebase-realtime-database

下面是我两项活动的代码。通过这样的编码,数据将被存储在firebase中。这是存储数据的快照:

this is the Snap of stored data

但是我想像这样将数据存储在firebase中,我想像这样将它们存储在注册号字段下:

this is how i want data

我想将来自多个活动的数据存储在同一子元素下,如我在第二个屏幕截图中所示。

AccountyActivity.java

 private void init() {

    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    mSettingsClient = LocationServices.getSettingsClient(this);

    mLocationCallback = new LocationCallback() {
        @Override
        public void onLocationResult(LocationResult locationResult) {
            super.onLocationResult(locationResult);
            // location is received
            mCurrentLocation = locationResult.getLastLocation();
            lot = mCurrentLocation.getLatitude();
            lgt = mCurrentLocation.getLongitude();
            lg = Double.toString(lgt);
            lt = Double.toString(lot);
            mAuth = FirebaseAuth.getInstance();
            FirebaseUser user = mAuth.getCurrentUser();

            UID = user.getUid();
            final DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://smartattendance-c896a.firebaseio.com/Users");

            DatabaseReference user1 = myRootRef.child(UID);
            user1.child("Longitude").setValue(lg);
            user1.child("Latitude").setValue(lt);

SignUpActivity.java

private FirebaseAuth mAuth;

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

    mAuth = FirebaseAuth.getInstance();

    final DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://smartattendance-c896a.firebaseio.com/Users");
    f1 = (EditText) findViewById(R.id.newUserName);
    f2 = (EditText) findViewById(R.id.newPassword);
    signUp = (Button) findViewById(R.id.signupbtn);
    signInLink = (TextView) findViewById(R.id.signinlink);
    tnc = (CheckBox) findViewById(R.id.tnc);
    f3 = (EditText) findViewById(R.id.cfmpassword);
    f4 = (EditText) findViewById(R.id.enroll);
    signUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startSignup();
            String Enrollment = f4.getText().toString();

            String email = f1.getText().toString();
            String pass = f2.getText().toString();
            DatabaseReference user = myRootRef.child(Enrollment);
            user.child("Email").setValue(email);
            user.child("Password").setValue(pass);
        }
    });

2 个答案:

答案 0 :(得分:1)

将值从一项活动传递到另一项活动:

SignupActivity 中:

Intent i=new Intent(SignUpActivity.this,AccountyActivity.class);
                    i.putExtra("id",Enrollment);

然后在 AccountyActivity 中执行以下操作:

String id = getIntent().getStringExtra("id");
DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference("Users");
        DatabaseReference ref = myRootRef.child(id);

        Map<String, Object> childUpdates = new HashMap<>();
         childUpdates.put("Longitude", lg);
         childUpdates.put("Latitude",lt);

        ref.updateChildren(childUpdates);

您需要使用updateChildren,以免覆盖其他子节点。

https://firebase.google.com/docs/database/android/read-and-write#update_specific_fields

答案 1 :(得分:0)

我有建议给你。您必须更改一些内容。 1.制作一个类来编写新数据(注册活动)

例如这样的类。

UsersInformation.java

package <replace with your package>;
public class UsersInformation {

private long enrollment;
private String email;
private String password;
//Maybe you want to use double, change it to double
private float latitude;
private float longitude;

public UsersInformation() {
}

public UsersInformation(long enrollment, String email, String password, float latitude, float longitude) {
    this.enrollment = enrollment;
    this.email = email;
    this.password = password;
    this.latitude = latitude;
    this.longitude = longitude;
}

public long getEnrollment() {
    return enrollment;
}

public void setEnrollment(long enrollment) {
    this.enrollment = enrollment;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public float getLatitude() {
    return latitude;
}

public void setLatitude(float latitude) {
    this.latitude = latitude;
}

public float getLongitude() {
    return longitude;
}

public void setLongitude(float longitude) {
    this.longitude = longitude;
}
}

SignUpActivity.java

private FirebaseAuth mAuth;
private FirebaseDatabase firebaseDatabase;
private DatabaseReference mRefUsers;

private UsersInformation userInfo;

private long enrollment;
private String email;
private String password;
//Maybe you want to use double, change it to double
private float latitude;
private float longitude;


private void writeNewUser(long enrollment, String email, String password, float latitude, float longitude){
userInfo = new UsersInformation(enrollment, email, password, latitude, longitude);
mRefUsers.child("Users").setValue(userInfo);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
mAuth = FirebaseAuth.getInstance();
firebaseDatabase = FirebaseDatabase.getInstance();
mRefUsers = FirebaseDatabase.getReference();


f1 = (EditText) findViewById(R.id.newUserName);
f2 = (EditText) findViewById(R.id.newPassword);
signUp = (Button) findViewById(R.id.signupbtn);
signInLink = (TextView) findViewById(R.id.signinlink);
tnc = (CheckBox) findViewById(R.id.tnc);
f3 = (EditText) findViewById(R.id.cfmpassword);
f4 = (EditText) findViewById(R.id.enroll);
signUp.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startSignup();
        enrollment = f4.getText().toString();
        email = f1.getText().toString();
        password = f2.getText().toString();
        latitude = 0.0
        longitude = 0.0

        writeNewUser(enrollment, email, password, latitude, longitude);
        Toast.makeText(getBaseContext(), "Successfully Registered! Proceed to Main Menu...", Toast.LENGTH_LONG).show();
        //Proceed to Accounty Acitivty
        Intent intent = new Intent(getContext(), AccountyActivity.class);
        intent.putExtra("enrollment", enrollment);
        getContext().startActivities(new Intent[]{intent});
    }
});

AccountyActivity.java

private void init() {

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);

mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult);
        // location is received
        mCurrentLocation = locationResult.getLastLocation();
        lot = mCurrentLocation.getLatitude();
        lgt = mCurrentLocation.getLongitude();
        lg = Double.toString(lgt);
        lt = Double.toString(lot);
        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        UID = user.getUid();

        Intent intent = this.getIntent();
        //Store value
        float enrollment = intent.getExtras().getString("enrollment");

        final DatabaseReference myRootRef = FirebaseDatabase.getInstance().getReference().child("Users").child(enrollment);

        DatabaseReference latitudeRef = myRootRef.child("latitude");
        latitudeRef.setValue(lg);

        DatabaseReference longitudeRef = myRootRef.child("longitude");
        longitudeRef.setValue(lt);

也许您需要检查几件事。