gpt4 book ai didi

android - 如何根据存储在 firebase 中的数据更改按钮文本颜色

转载 作者:行者123 更新时间:2023-11-30 04:55:03 24 4
gpt4 key购买 nike

我目前正在使用 Firebase 在 Android 中做我的 parking 系统项目。

enter image description here

这是我的输出,显示了 parking 场中的 parking 位..

此输出中的每个按钮代表批处理中的一个插槽。这些按钮是根据每个批处理存储在数据库中的“插槽数”(AllSlot 集合中的 SlotNo4)动态创建的。

如果插槽“可用”(如数据库中为每个插槽提供的那样(AllSlot 集合中的 SStatus)),我希望这些按钮文本颜色为黑色,如果插槽已被“占用”,则为红色车辆,如果插槽已“预订”,则为黄色。

但我无法弄清楚..正如您在输出中看到的那样,只有“S5”以黄色显示..其余的插槽(按钮)文本颜色没有改变..请帮助我

enter image description here

这是我的 Firebase 数据库集合 (AllSlot),用于插槽详细信息..

enter image description here

数据库截图续

这是我的代码。

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) {

}
});

}
}

最佳答案

写一个类似这样的方法:

private 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);
}
}
}

并为您所有的父布局调用它,为什么您的方法不起作用,因为您有一个全局变量 b 而您只是每次都为其分配一个新按钮并丢失旧引用。

关于android - 如何根据存储在 firebase 中的数据更改按钮文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59359209/

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