我正在从Firebase中提取Boolean
。但是,提取数据时出现Can't convert object of type java.lang.Boolean
错误。
List<Product> productList;
List<Boolean> productListStates;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states").child("001");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Product p = snapshot.getValue(Product.class);
productListStates.add(p.getStates());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
产品类别:
public class Product {
private String places;
private String time;
private String title;
private String id;
private boolean States;
public Product(){
}
public Product(String places, String time, String title, String id, boolean States) {
this.places = places;
this.time = time;
this.title = title;
this.id = id;
this.States = States;
}
public String getplaces() {
return places;
}
public String gettime() {
return time;
}
public String gettitle() {
return title;
}
public String getid() {
return id;
}
public boolean getStates() {
return States;
}
}
答案 0 :(得分:0)
正如我在您的架构中看到的那样,在states
节点下没有Product
类型的对象,只是布尔类型的属性。这就是为什么您会收到该错误。要解决此问题,请使用以下代码行:
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states").child("001");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
booblean b = snapshot.getValue(Boolean.class);
Log.d(TAG, String.valueOf(b));
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
logcat中的结果将是:
true