Android 如何根据firebase中存储的数据更改按钮文本颜色
我目前正在用Firebase做我的Android停车系统项目 这是我的输出,显示了停车场的车位 此输出中的每个按钮代表一个批次中的一个插槽。这些按钮是根据存储在数据库中的每个批次的“插槽数”(AllSlot集合中的插槽数4)动态创建的 如果插槽“可用”(如数据库中为每个插槽(AllSlot collection中的SStatus)提供的),我希望这些按钮文本颜色为黑色;如果插槽已被车辆“占用”,则这些按钮文本颜色为红色;如果插槽已“预订”,则这些按钮文本颜色为黄色 但是我看不出来。正如您在输出中看到的,只有“S5”显示为黄色。其余插槽(按钮)文本颜色未更改。请帮助我 这是我的Firebase数据库集合(AllSlot),用于获取插槽详细信息 数据库截图延续 这是我的密码Android 如何根据firebase中存储的数据更改按钮文本颜色,android,firebase-realtime-database,Android,Firebase Realtime Database,我目前正在用Firebase做我的Android停车系统项目 这是我的输出,显示了停车场的车位 此输出中的每个按钮代表一个批次中的一个插槽。这些按钮是根据存储在数据库中的每个批次的“插槽数”(AllSlot集合中的插槽数4)动态创建的 如果插槽“可用”(如数据库中为每个插槽(AllSlot collection中的SStatus)提供的),我希望这些按钮文本颜色为黑色;如果插槽已被车辆“占用”,则这些按钮文本颜色为红色;如果插槽已“预订”,则这些按钮文本颜色为黄色 但是我看不出来。正如您在输出
public class SlotActivity extends AppCompatActivity implements View.OnClickListener {
LinearLayout parent1, parent2, parent3, parent4;
Button b;
String m = Prevalent1.currentsearchslot.getSlotNo4();
int n = Integer.parseInt(m);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("AllSlot").
child(Prevalent1.currentsearchslot.getLID());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slot);
parent1 = (LinearLayout) findViewById(R.id.ll_parent);
parent2 = (LinearLayout) findViewById(R.id.ll_parent2);
parent3 = (LinearLayout) findViewById(R.id.ll_parent3);
parent4 = (LinearLayout) findViewById(R.id.ll_parent4);
for (int i = 0; i < n / 4; i++) {
b = new Button(SlotActivity.this);
b.setId(i + 1);
b.setText("S" + (i + 1));
slotdb(b.getId());
parent1.addView(b);
b.setOnClickListener(SlotActivity.this);
}
for (int i = n / 4; i < n / 2; i++) {
b = new Button(SlotActivity.this);
b.setId(i + 1);
b.setText("S" + (i + 1));
slotdb(b.getId());
parent2.addView(b);
b.setOnClickListener(SlotActivity.this);
}
for (int i = n / 2; i < 3 * n / 4; i++) {
b = new Button(SlotActivity.this);
b.setId(i + 1);
b.setText("S" + (i + 1));
slotdb(b.getId());
parent3.addView(b);
b.setOnClickListener(SlotActivity.this);
}
for (int i = 3 * n / 4; i < n; i++) {
b = new Button(SlotActivity.this);
b.setId(i + 1);
b.setText("S" + (i + 1));
slotdb(b.getId());
parent4.addView(b);
b.setOnClickListener(SlotActivity.this);
}
}
private void slotdb(final int id) {
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("AllSlot").
child(Prevalent1.currentsearchslot.getLID());
final String sid = String.valueOf(id);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (!(dataSnapshot.child(sid).exists())) {
HashMap<String, Object> userdataMap = new HashMap<>();
userdataMap.put("SID", sid);
userdataMap.put("SStatus", "Available");
ref.child(sid).updateChildren(userdataMap);
} else {
if (dataSnapshot.child(sid).child("SStatus").exists()) {
if ((dataSnapshot.child(sid).child("SStatus")).getValue().equals("Occupied")) {
b.setTextColor(Color.RED);
} else if ((dataSnapshot.child(sid).child("SStatus")).getValue().equals("Booked")) {
b.setTextColor(Color.YELLOW);
} else {
b.setTextColor(Color.BLACK);
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onClick(final View view) {
view.getId();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("AllSlot").
child(Prevalent1.currentsearchslot.getLID());
final int a = view.getId();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.child(String.valueOf(a)).exists()) {
SingleSlot data = dataSnapshot.child(String.valueOf(a)).getValue(SingleSlot.class);
if (data.getSID().equals(String.valueOf(a))) {
Prevalent2.currentclickedslot = data;
if (Prevalent2.currentclickedslot.getSStatus().equals("Available")) {
Toast.makeText(getApplicationContext(), "Available!!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(SlotActivity.this, BookInfo.class);
startActivity(intent);
} else if (Prevalent2.currentclickedslot.getSStatus().equals("Occupied")) {
Toast.makeText(getApplicationContext(), "Occupied!!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "This Slot is already Booked!!", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
公共类SlotActivity扩展了AppCompatActivity实现了View.OnClickListener{
线性布局父母1、父母2、父母3、父母4;
按钮b;
字符串m=Prevalent1.currentsearchslot.getSlotNo4();
int n=Integer.parseInt(m);
DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child(“AllSlot”)。
child(Prevalent1.currentsearchslot.getLID());
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_插槽);
parent1=(LinearLayout)findViewById(R.id.ll_parent);
parent2=(LinearLayout)findViewById(R.id.ll_parent2);
parent3=(LinearLayout)findViewById(R.id.ll\u parent3);
parent4=(LinearLayout)findViewById(R.id.ll_parent4);
对于(int i=0;iprivate void changeTextColor(LinearLayout layout, int color) {
for (int i = 0; i < layout.getChildCount(); i++) {
View v = layout.getChildAt(i);
if (v instanceof Button) {
v.setTextColor(color);
}
}
}