【发布时间】:2019-03-05 09:14:19
【问题描述】:
我想使用 Android 应用更改 Firebase 数据库中的数据。所以我希望它在打开时为真,在我关闭时为假。 (0-states-001-True/False)所以当我按下切换按钮时,我想更新数据(true/false))。我分享了下面的代码。如何更新数据?这些代码不起作用,数据是错误的。 database application
DatabaseReference dbProducts1 = FirebaseDatabase.getInstance().getReference("0").child("states").child("001");
dbProducts1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot1) {
if(dataSnapshot1.exists()){
for(DataSnapshot productSnapshot : dataSnapshot1.getChildren()){
Product1 p1 = productSnapshot.getValue(Product1.class);
productList.add(p1);
}
adapter = new ProductsAdapter(MainActivity.this, productList);
recyclerView.setAdapter(adapter);
}
}
public class Product1 extends Product {
private boolean states;
public Product1(){
}
public Product1(boolean states) {
this.states = states;
}
public boolean getStates() {
return states;
}
}
public void onBindViewHolder(ProductViewHolder holder, int position) {
Product product = productList.get(position);
// holder.textViewID.setText(String.valueOf(product.getid()));
holder.viewStatus.setText(String.valueOf(product.getStates()));
}
class ProductViewHolder extends RecyclerView.ViewHolder {
ToggleButton viewStatus;
public ProductViewHolder(View itemView) {
super(itemView);
viewStatus = itemView.findViewById(R.id.toggle_Data);
}
}
【问题讨论】:
标签: android database firebase togglebutton