我正在为Windows构建一个FMX应用程序,该应用程序具有一个按钮和一个TListView控件。按钮代码只是对用于填充TListView的SQLite数据库的查询。在某些情况下,查询需要5到10秒钟来处理。我想在单击按钮时立即清除TListView(在查询完成并重新填充列表之前)。在按钮代码的第一个中,我调用了Clear方法,但无法立即使用:
ListView1->Clear();
旧数据将保留在TListView控件中,直到查询完成并刷新它。我如何强制列表首先清空。
谢谢, 鲁斯
已更新以清楚显示正在发生的事情:
// This entire block of code below is inside a button click.
Form1->ListView1->Clear();
// This next line of code is what Frederico suggested - it works.
Application->ProcessMessages();
Form1->FDConnection1->Params->Values["Database"] = "T:\\mydata.db";
TFDQuery *query;
query = new TFDQuery(NULL);
query->Connection = Form1->FDConnection1; // SQLite db
query->SQL->Text = "SELECT DISTINCT Project FROM Engineer_Updates WHERE Employee = '" + Form1->lblEngineerInitials->Text + "' ORDER BY Project";
query->Open();
while(!query->Eof) {
TListViewItem* itemHeader = Form1->ListView1->Items->Add();
Form1->ListView1->BeginUpdate();
itemHeader->Purpose = TListItemPurpose::Header;
itemHeader->Text = query->FieldByName("Project")->AsString; // + " - " + query->FieldByName("Project WO")->AsString; // "My Header";
Form1->ListView1->EndUpdate();
TFDQuery *query2;
query2 = new TFDQuery(NULL);
try {
query2->Connection = Form1->FDConnection2; // SQLite db
query2->SQL->Text = "SELECT * FROM Engineer_Updates WHERE Project = '" + query->FieldByName("Project")->AsString + "' AND Employee = '" + Form1->lblEngineerInitials->Text + "'";
query2->Open();
while (!query2->Eof) {
TListViewItem* item2Add = Form1->ListView1->Items->Add();
Form1->ListView1->BeginUpdate();
item2Add->Text = query2->FieldByName("Terminal/Equipment")->AsString + ", " + query2->FieldByName("Activity")->AsString;
mystring2 = query2->FieldByName("Terminal/Equipment")->AsString + ", " + query2->FieldByName("Activity")->AsString;
item2Add->Detail = mystring2;
Form1->ListView1->EndUpdate();
query2->Next();
}
}
__finally {
query2->Close();
query2->DisposeOf();
}
query->Next();
}
query->Close();
query->DisposeOf();
答案 0 :(得分:0)
可能的解决方案(使用Delphi代码):
ListView1.Clear;
//eventually show a spinning wheel here, so the user knows something is going on.
ListView1.InvalidateRect(ListView1.BoundsRect); //<- might not be needed, try first without
TThread.ForceQueue(nil, procedure
begin
//execute your query here and fill the listview
//stop the spinning wheel
end;
答案 1 :(得分:0)
TListView是否通过BindSource绑定了?如果是,请尝试使用单独的线程(例如匿名线程)清除数据并禁用bindsource中的数据集
BindSourceDB1.DataSet.Active := False;
TThread.CreateAnonymousThread(procedure ()
begin
// delete data here using separated query
TThread.Synchronize(nil, procedue ()
begin
BindSourceDB1.DataSet.Active := True;
end);
end).Start;