如何在 ListView 中显示图像取决于日期
我正在使用 Delphi Sydney 10.4 FMX,我有一个 ListView 通过 liveBinding 连接到数据库,ImageList 有 3 个图像。
在 ListView 中,我有 3 个字段:Image、Expiry、Domain。
到期日和域是从数据库填充的,但我要显示的图像取决于日期,例如:
到期日 = 今天或今天之前的日期:我想显示 imageindex 0
到期日 = 从明天到今天的 30 天:我想要显示 imageindex 1
到期 = 从今天起 31 天后:我想显示 imageindex 2
procedure TForm1.DomainsListViewUpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
begin
var DT : TDateTime;
DT := Now;
if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString < datetostr(DT+30) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 1
else if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString < DateToStr(DT) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 0
else if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString > DateToStr(DT+31) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 2
end;
我使用了此代码,但无法正常工作
I'm Using Delphi Sydney 10.4 FMX, I have a ListView connected to a Database with liveBinding, ImageList that has 3 images.
In the ListView, I Have 3 Fields: Image, Expiry, Domain.
The Expiry and Domain are filled from the Database, but the image I want to show depends on the date, example :
Expiry = date of today or before today: I want to show imageindex 0
Expiry = from Tomorrow until 30 days from Today: I want to show imageindex 1
Expiry = 31 Days Later from today: I want to show imageindex 2
procedure TForm1.DomainsListViewUpdateObjects(const Sender: TObject;
const AItem: TListViewItem);
begin
var DT : TDateTime;
DT := Now;
if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString < datetostr(DT+30) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 1
else if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString < DateToStr(DT) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 0
else if DomainsListView.Items[DomainsListView.ItemIndex].Data['expiry'].AsString > DateToStr(DT+31) then
DomainsListView.Items[DomainsListView.ItemIndex].Data['image'] := 2
end;
I used this code, but does not works correctly
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在比较
String
值,这不适用于比较日期。您应该比较TDateTime
值,并更加注意比较的顺序。尝试更像这样的事情:
You are comparing
String
values, which doesn't work to compare dates. You should be comparingTDateTime
values instead, as well as paying more attention to the order of your comparisons.Try something more like this instead: