gpt4 book ai didi

java - 更改 ListView 项的颜色

转载 作者:行者123 更新时间:2023-12-05 00:46:27 24 4
gpt4 key购买 nike

我有一个 ListView ,其中包含有关我的客户的信息。它基本上是已预订工作的用户列表。 ListView 使用 firebase 数据库作为其来源。它连接到一个名为 Bookings 的节点。然后它将用户 ID 作为其 key 。

{
"Bookings": {
"User-Id appears here": {
"Fullname": "Name Appears here",
"Date": "",
"Valet Type": "",
"Car Model": "",
"CustomerID": "",
"Approved": ""
}
}
}

Approved 节点可以是“Not Approved”、“Approved”、“Canceled”这三个值之一根据批准的值,我想更改包含数据的 ListView 项的颜色。我已经设法让 ListView 在单击该项目时更改颜色,但我希望它保持该颜色,除非“已批准”节点另有说明。

这是我目前拥有的代码,当点击该项目时它会发生变化。

bookingsref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
for (final DataSnapshot snapshot : dataSnapshot.getChildren()) {

date = snapshot.child("Date").getValue().toString();
name = snapshot.child("Fullname").getValue().toString();
valet = snapshot.child("ValetType").getValue().toString();
model = snapshot.child("VehicleModel").getValue().toString();
customerid = snapshot.child("UserId").getValue().toString();

confirm = snapshot.child("Approved").getValue().toString();
ApproveArray.add(confirm);
bookingID.add(customerid);

booking_info_minor.add(date + "\n" + "Name: "+ name + "\n"+"Valet Type: "+valet + "\n" +"Model: " + model + "\n"+"Customer ID: "+ customerid );

adapter =new ArrayAdapter<String>(Bookings .this,android.R.layout.simple_list_item_1,booking_info_minor);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String c = bookingID.get(position).toString();
confirm = ApproveArray.get(position);


Intent intent=new Intent(getBaseContext(),Booking_details.class);
Bundle bundle = new Bundle();
bundle.putString( "UserId",c);
intent.putExtras(bundle);
startActivity(intent);
listColour(position,confirm);


}
});

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
}

private void listColour(int pos,String confirm){
Log.i("Conf",confirm);
if (confirm.equals("Approved")){
lv = findViewById(R.id.templist);
lv.getChildAt(pos).setBackgroundResource(R.color.approved);
}
if (confirm.equals("Not Approved")) {
lv = findViewById(R.id.templist);
lv.getChildAt(pos).setBackgroundResource(R.color.not_approved);
}

if (confirm.equals("Canceled")) {
lv = findViewById(R.id.templist);
lv.getChildAt(pos).setBackgroundResource(R.color.canceled);
}

最佳答案

只需覆盖 ArrayAdapter 的getView 并进行如下适当的更改:

adapter = new ArrayAdapter<String>(Bookings.this, android.R.layout.simple_list_item_1, booking_info_minor) {
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

View view = super.getView(position, convertView, parent);

String confirm = ApproveArray.get(position);

switch (confirm) {
case "Approved":
view.setBackgroundResource(R.color.approved);
break;
case "Not Approved":
view.setBackgroundResource(R.color.not_approved);
break;
case "Canceled":
view.setBackgroundResource(R.color.canceled);
break;
}

return view;
}
};

关于java - 更改 ListView 项的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59859469/

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