2 回答

TA貢獻(xiàn)1852條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以直接將您的模型類與 Firebase 響應(yīng)數(shù)據(jù)進(jìn)行映射。您不必手動(dòng)設(shè)置模型類中的每個(gè)數(shù)據(jù)。
如果您的數(shù)據(jù)將從 Firebase 中成功檢索,則此代碼將起作用,請嘗試以下代碼。
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clienti);
Utente=findViewById(R.id.tvUtente);
ApeOmaggio = findViewById(R.id.tvOmaggio);
bInfo= new ButtonInformation();
img1=findViewById(R.id.img1);
img2=findViewById(R.id.img2);
img3=findViewById(R.id.img3);
img4=findViewById(R.id.img4);
img5=findViewById(R.id.img5);
img6=findViewById(R.id.img6);
img7=findViewById(R.id.img7);
img8=findViewById(R.id.img8);
img9=findViewById(R.id.img9);
mAuth=FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
user = mAuth.getCurrentUser();
utenteLog = user.getDisplayName();
mail = user.getEmail();
mailNoSpace=mail.replaceAll("\\.","");
Utente.setText(utenteLog);
myRef=mFirebaseDatabase.getReference(mailNoSpace);
myRef.addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ButtonInformation buttonInformation = dataSnapshot.getValue(ButtonInformation.class);
showData(buttonInformation);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void showData(ButtonInformation buttonInformation) {
if (buttonInformation.getButton1()==true){
img1.setImageResource(R.drawable.berry_icon);
}else{
img1.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton2()==true){
img2.setImageResource(R.drawable.berry_icon);
}else{
img2.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton3()==true){
img3.setImageResource(R.drawable.berry_icon);
}else{
img3.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton4()==true){
img4.setImageResource(R.drawable.berry_icon);
}else{
img4.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton5()==true){
img5.setImageResource(R.drawable.berry_icon);
}else{
img5.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton6()==true){
img6.setImageResource(R.drawable.berry_icon);
}else{
img6.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton7()==true){
img7.setImageResource(R.drawable.berry_icon);
}else{
img7.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton8()==true){
img8.setImageResource(R.drawable.berry_icon);
}else{
img8.setImageResource(R.drawable.radio_button);
}
if (buttonInformation.getButton9()==true){
img9.setImageResource(R.drawable.berry_icon);
}else{
img9.setImageResource(R.drawable.radio_button);
}
ApeOmaggio.setText(Integer.toString(bInfo.getApeOmaggio()));
}

TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個(gè)贊
在訪問 firebase 之前使用 if not null 條件?;蛘?,如果您已將子屬性更改為您的 firebase 項(xiàng)目,請檢查樹。
添加回答
舉報(bào)