我用TThread.CreateAnonymousThread
从TRestRequest
填充了一个TListView
。第一次运行时一切正常,但在PulltoRefresh
上不起作用(没有冻结,没有结果(。如果我不使用Threads,它可以很好地处理冻结。
当我使用除了ListView.Items.Clear
之外的相同代码时,问题在哪里?
这是我的密码。
procedure TForm4.Server;
var
i, Id: Integer;
UName, Descr,ThumbUrl: UnicodeString;
LItem: TListViewItem;
begin
FDMemTable1.EmptyDataSet;
TThread.CreateAnonymousThread(
procedure
begin
RESTClient1.BaseURL := {my PHP URL};
RESTRequest1.Execute;
FDMemTable1.First;
TThread.Synchronize(nil,
procedure
var
i: integer;
begin
for i := 0 to FDMemTable1.RecordCount - 1 do
begin
Id := FDMemTable1.FieldByName('id').Value;
UName := FDMemTable1.FieldByName('username').Value;
Descr := FDMemTable1.FieldByName('descr').Value;
ThumbUrl := FDMemTable1.FieldByName('image_url').Value;
ListView1.BeginUpdate;
LItem := ListView1.Items.Add;
LItem.Data['Id'] := Id;
LItem.Data['UName'] := UName;
LItem.Data['Descr'] := Descr;
LItem.Data['ThumbUrl'] := ThumbUrl;
LItem.Adapter.ResetView(LItem);
ListView1.EndUpdate;
FDMemTable1.Next;
end;
end
);
end
).Start;
end;
procedure TForm4.FormShow(Sender: TObject);
begin
LoadFormState;
Server;
end;
procedure TForm4.ListView1PullRefresh(Sender: TObject);
begin
ListView1.BeginUpdate;
try
ListView1.Items.Clear;
finally
ListView1.EndUpdate;
end;
Server;
end;
我不得不提到在ListView1UpdatingObjects
上创建的项目。我在安卓系统上测试它。
最后,我必须先清除ListView1.Items位图,然后删除该项,如下所示。
procedure TForm4.ListView1PullRefresh(Sender: TObject);
var
i: integer;
AItem: TListViewItem;
iBitmap: TListItemImage;
begin
ListView1.BeginUpdate;
try
ListView1.OnPaint := nil;
while ListView1.Items.Count > 0 do
begin
iBitmap := ListView1.Items[0].Objects.FindObjectT<TListItemImage>('MImage');
if (Assigned(iBitmap) and iBitmap.OwnsBitmap) then
begin
iBitmap.Bitmap.Free;
iBitmap.Bitmap := nil;
end;
ListView1.Items.Delete(0);
end;
finally
ListView1.EndUpdate;
Server;
end;
接下来,在代码中的某个位置设置ListView1.OnPaint := ListView1Paint
;