通过java类将数据上传到firebase,并在另一个活动中检索一个属性

时间:2018-05-05 22:01:25

标签: java android json firebase firebase-realtime-database

Firebase Database Link我正在尝试使用此代码将数据保存到firebase。 一切正常。我正在保存创建新孩子用户的数据> uid。但现在当我尝试通过创建一个对象并以setvalue传递该对象来保存数据时,应用程序崩溃了。请帮忙。

public class RegComplete extends AppCompatActivity {

private EditText Name, Surname, Address, Tel, DateOfBirth,Username;
private String _name, _surname, _address, _Tel, _DateOfBirth,_UserName, email;
public String uid = "";

FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference mdatabase;
private DatabaseReference fdatabase;
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser objuser = auth.getCurrentUser();

List<String> lstSports = new ArrayList<String>();
List<String> lstSteden = new ArrayList<String>();
String strSport1,strSport2,strSport3;
public String strCity;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_reg_complete);

    fdatabase = database.getReference();

    fdatabase.child("Steden").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot DS:dataSnapshot.getChildren()) {
                String CityName = DS.child("CityName").getValue(String.class);
                City stad = new City(CityName);
                lstSteden.add(stad.CityName);
            }
            final Spinner CitySpin = (Spinner) findViewById(R.id.CitySpin);
            ArrayAdapter<String> CityAdapter = new ArrayAdapter<String>(RegComplete.this, android.R.layout.simple_spinner_item, lstSteden);
            CityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            CitySpin.setAdapter(CityAdapter);
            CitySpin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    strCity = CitySpin.getSelectedItem().toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });




    fdatabase.child("SPORTS").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds :dataSnapshot.getChildren()) {
                String sportName = ds.child("SportName").getValue(String.class);
                Sport objsport = new Sport(sportName);

                lstSports.add(objsport.SportName);
            }

            final Spinner Sports1 = (Spinner) findViewById(R.id.SportSpinner1);
            ArrayAdapter<String> SportAdapter = new ArrayAdapter<String>(RegComplete.this, android.R.layout.simple_spinner_item, lstSports);
            SportAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            Sports1.setAdapter(SportAdapter);
            Sports1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    strSport1 = Sports1.getSelectedItem().toString();

                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
            final Spinner Sports2 = (Spinner) findViewById(R.id.SportSpinner2);
            Sports2.setAdapter(SportAdapter);
            Sports2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    strSport2 = Sports2.getSelectedItem().toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });

            final Spinner Sports3 = (Spinner) findViewById(R.id.SportSpinner3);
            Sports3.setAdapter(SportAdapter);
            Sports3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    strSport3 = Sports3.getSelectedItem().toString();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    Name = findViewById(R.id.NameeditText);
    _name = Name.getText().toString();
    Surname = findViewById(R.id.SurnameEditText);
    _surname = Surname.getText().toString();
    Address = findViewById(R.id.AdressEditText);
    _address = Address.getText().toString();
    Tel = findViewById(R.id.TelEditText);
    _Tel = Tel.getText().toString();
    DateOfBirth = findViewById(R.id.DOBEditText);
    _DateOfBirth = DateOfBirth.getText().toString();
    Username = findViewById(R.id.UserIDEditText);
    _UserName = Username.getText().toString();
    email = FirebaseAuth.getInstance().getCurrentUser().getEmail().toString();


}
public void btnSave_Click(View V){
    User usinfo = new User(email,_UserName,_name,_surname,_address,strCity,_Tel,_DateOfBirth,strSport1,strSport2,strSport3,uid);

    mdatabase = FirebaseDatabase.getInstance().getReference("USERS");
    uid = objuser.getUid();
    mdatabase.child(uid).setValue(usinfo);

    Intent i = new Intent(RegComplete.this, LoginActivity.class);
    Toast.makeText(RegComplete.this ,"Registration Complete" ,Toast.LENGTH_SHORT).show();
    startActivity(i);
}

}

USER.class的代码

public class User {

public String Email;
public String UserName;
public String Name;
public String SurName;
public String StreetAdress;
public String City;
public String Tel;
public String DOB;
public String Sport1;
public String Sport2;
public String Sport3;
public String UserId;
public User(){
    // Default constructor required by Firebase
}

public User(String email, String userName, String name, String surName, String streetAdress, String city, String tel, String DOB, String sport1, String sport2, String sport3, String userId) {
    Email = email;
    UserName = userName;
    Name = name;
    SurName = surName;
    StreetAdress = streetAdress;
    City = city;
    Tel = tel;
    this.DOB = DOB;
    Sport1 = sport1;
    Sport2 = sport2;
    Sport3 = sport3;
    UserId = userId;
}

public String getEmail() {
    return Email;
}

public String getUserName() {
    return UserName;
}

public String getName() {
    return Name;
}

public String getSurName() {
    return SurName;
}

public String getStreetAdress() {
    return StreetAdress;
}

public String getCity() {
    return City;
}

public String getTel() {
    return Tel;
}

public String getDOB() {
    return DOB;
}

public String getSport1() {
    return Sport1;
}

public String getSport2() {
    return Sport2;
}

public String getSport3() {
    return Sport3;
}

public String getUserId() {
    return UserId;
}

}

我还试图为我在另一个活动中登录的用户检索UserName。 使用此代码。

   mdatabase.child("USERS").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                userinf = ds.child(uid).getValue(User.class);
            };

            txtUserNaam.setText(userinf.UserName);
        }

哪里

DatabaseReference mdatabase = FirebaseDatabase.getInstance().getReference();

请帮忙

1 个答案:

答案 0 :(得分:0)

Firebase在这个领域之间变得混乱:

public String City;

这个吸气者:

public String getCity() {
    return City;
}

该字段定义属性City,而getter定义属性city。正如您所看到的,两者之间的情况不同,这会导致错误消息。

最简单的解决方法是将所有字段标记为私有:

private String City;

这使得Firebase在阅读/写入用户时不会考虑它们。