2 回答
TA贡献1852条经验 获得超7个赞
您可以直接将您的模型类与 Firebase 响应数据进行映射。您不必手动设置模型类中的每个数据。
如果您的数据将从 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()));
}
添加回答
举报