我想从数据库中提取布尔数据,但是我的代码是错误的。如何将布尔数据分配给getstate变量?我想将数据库中的数据分配给产品类中的getstate变量。我该怎么办?
我在行上收到此错误:
productList.add(p.getStates());
:我收到错误消息:
列表不能应用于布尔值
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
final DatabaseReference ref = mDatabase.child("0").child("states");
List<Product> productList;
dbProducts.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
productList = new ArrayList<>();
if(dataSnapshot.exists()){
for(DataSnapshot productSnapshot : dataSnapshot.getChildren()){
Product p = productSnapshot.getValue(Product.class);
productList.add(p);
}
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Product p = snapshot.getValue(Product.class);
productList.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 :(得分:2)
您试图在包含产品的列表中添加布尔值。正确的方法是:
productList.add(p);