我是Firebase数据库的新手,我不知道为什么无法从数据库将数据获取到对象中
当我从Firebase数据库中调用数据时,这将提供正确的数据,但是我无法将其设置为对象或文本视图的任何帮助,请 检查Firebase数据库的屏幕截图,这是我的数据库屏幕,以便如何从该类型的JSON获取数据
public class ProfileFrag extends Fragment {
TextView name,companyName,emailAddress, mobileNumber,companyAddress;
FirebaseDatabase database;
DatabaseReference myRef;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// init
name = view.findViewById(R.id.name);
companyName = view.findViewById(R.id.company_name);
emailAddress = view.findViewById(R.id.gmail_address);
mobileNumber = view.findViewById(R.id.mobile_number);
companyAddress = view.findViewById(R.id.address);
database = FirebaseDatabase.getInstance();
SharedPreferences sharedPreferences = getActivity().getSharedPreferences("Data", MODE_PRIVATE);
String key = sharedPreferences.getString("Key","");
myRef = FirebaseDatabase.getInstance().getReference().child(key).child("PersonalData");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
BossModel data = dataSnapshot.child("PersonalData").getValue(BossModel.class);
companyAddress.setText(data.getCompanyAddress());
name.setText(data.getName());
companyName.setText(data.getCompanyName());
emailAddress.setText(data.getEmailAddress());
mobileNumber.setText(data.getMobileNumber()); }
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
return view;
}
}
登录后我确实是这样
Map<String,Object> boss = new HashMap<>();
boss.put("Name",sFullName.getText().toString().trim());
boss.put("Company Name",sCompanyName.getText().toString().trim());
boss.put("Email Address",sEmailAddress.getText().toString().trim());
boss.put("Mobile Number",sMobileNumber.getText().toString().trim());
boss.put("Company Address",sCompanyAddress.getText().toString().trim());
String key = userRef.push().getKey();
SharedPreferences sharedPreferences = getSharedPreferences("Data", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Key",key);
editor.apply();
userRef.child(key).child("PersonalData").setValue(boss);
signIn();
我也正在附加Firebase数据库的屏幕截图firebase database screen
答案 0 :(得分:0)
您正在从错误的节点获取数据。
myRef = FirebaseDatabase.getInstance().getReference().child(key).child("PersonalData");
您已经在
key->PersonalData
通知:您确定数据库没有父节点吗?您能告诉我完整的数据库结构吗?如果确定,请进行如下更改。
myRef = FirebaseDatabase.getInstance().getReference().child(key).child("PersonalData");
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onValueChanged(@NonNull DataSnapshot dataSnapshot) {
//Here I made change
BossModel data = dataSnapshot.getValue(BossModel.class);
companyAddress.setText(data.getCompanyAddress());
name.setText(data.getName());
companyName.setText(data.getCompanyName());
emailAddress.setText(data.getEmailAddress());
mobileNumber.setText(data.getMobileNumber()); }
//The rest are the same.
注意:如果不能解决问题,我需要查看您的完整数据库结构。