gpt4 book ai didi

java - DatabaseException : can't convert object of java. Lang.String 要键入

转载 作者:行者123 更新时间:2023-12-02 10:33:58 27 4
gpt4 key购买 nike

我正在尝试从 Firebase 实时数据库检索数据,但遇到了此问题。

我怀疑由于 DatabaseReference 中的路径而导致此错误。我尝试了不同的路径,但似乎都不起作用。

这是我的数据库(生成的文本是用户ID)

enter image description here

我的存储数据结构的类

public class ERDataStructure {
private String temperature;
private String humidity;

public ERDataStructure() {

}
public ERDataStructure(String temperature, String humidity) {
this.temperature = temperature;
this.humidity = humidity;
}

public String getTemperature(){

return temperature;
}
public void setTemperature(String temperature){

this.temperature = temperature;
}

public String getHumidity() {

return humidity;
}
public void setHumidity(String humidity) {

this.humidity = humidity;
}
}

我的ReadingScreen.java(MainActivity)

import ...


public class ReadingScreen extends AppCompatActivity {

//Firebase Initialization
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private FirebaseDatabase mFirebaseDatabase;

private static final String TAG = "ReadingScreen";
ERDataStructure mData;
private TextView textViewTemperatureNumber, textViewHumidityNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading_screen);
//Find Views
textViewHumidityNumber = (TextView) findViewById(R.id.humidity_number);
textViewTemperatureNumber = (TextView) findViewById(R.id.temperature_number);
// Initiate Firebase
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String userID = user.getUid();

// Non-working Reference, but code works
//myRef = mFirebaseDatabase.getReference();
//myRef = mFirebaseDatabase.getReference().child(userID);

//Crashing Reference
myRef = mFirebaseDatabase.getReference().child(userID).child("ERDataStructure");
//myRef = mFirebaseDatabase.getReference("ERDataStructure");
//
retrieveData();
}
public void gotoMainMenu (View view)
{
Intent intent = new Intent(ReadingScreen.this, MainMenu.class);
startActivity(intent);
}
private void retrieveData(){
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
textViewTemperatureNumber.setText("" +ds.getTemperature());
textViewHumidityNumber.setText(""+ds.getHumidity());

}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
textViewTemperatureNumber.setText("" +ds.getTemperature());
textViewHumidityNumber.setText(""+ds.getHumidity());
}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
myRef.addValueEventListener(new ValueEventListener() {

@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<ERDataStructure> arraylist = new ArrayList<ERDataStructure>();
if (dataSnapshot != null && dataSnapshot.getValue() != null){
//FirebaseUser user = mAuth.getCurrentUser();
//String userID = user.getUid();
for (DataSnapshot a : dataSnapshot.getChildren()){

ERDataStructure dataStructure = new ERDataStructure();
dataStructure.setTemperature(a.getValue(ERDataStructure.class).getTemperature());
dataStructure.setHumidity(a.getValue(ERDataStructure.class).getHumidity());
arraylist.add(dataStructure);
}
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}
}

错误记录在

ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);

编辑:向我的数据库添加另一个级别后,它设法运行

enter image description here

最佳答案

您需要使用ValueEventListener而不是ChildEventListener,因此将其更改为:

myRef = mFirebaseDatabase.getReference().child(userID).child("ERDataStructure");

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ERDataStructure ds = dataSnapshot.getValue(ERDataStructure.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});

在本例中,您使用 ValueEventListener,因为当您使用 childEventListener 时,您基本上是在此快照中循环,并返回 humidity 的值,如下所示输入字符串。这就像在 valueventlistener 中循环 dataSnapshot.getChildren()

关于java - DatabaseException : can't convert object of java. Lang.String 要键入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53436235/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com