Delphi - TListView Thread Lazy-Loading

453 views
Skip to first unread message

Molik Tayari

unread,
May 17, 2014, 9:06:57 AM5/17/14
to
What exactly i need achieve is showin at this link below. (https://www.youtube.com/watch?v=B63_mczn05s)

It is already one week that i am engaging on it but it is not working. I do parsing the information and afterward i do follow normal listing step. Unfortunately there occurs a problem while loading the pictures at the same time and it does not place each picture to the requested Itemindex. Even sometimes it does not load the picture. I am trying to know where is the main problem in my Thread tool.
-------------------------------------------------------------------------------
.............

type
ListThread = class(TThread)
private

procedure UpdateListThread;
protected
procedure Execute; override;
public
price, name, ID, objCount: integer;
constructor Create(Suspended: Boolean);
end;

type
TDownload = Class(TThread)
Private
URL: string;
MS: TMemoryStream;
Bitmap: TImage;
ID, XIndex: string;
Sonuc: integer;
procedure UpdateDownloadThread;
Public
Procedure Execute; Override;
End;

type
TForm1 = class(TForm)
ToolBar1: TToolBar;
AniIndicator1: TAniIndicator;
Image1: TImage;
ActionList1: TActionList;
GestureManager1: TGestureManager;
Action1: TAction;
ListView1: TListView;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
procedure FormCreate(Sender: TObject);
procedure Action1Execute(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
{ Private declarations }
procedure EndThread(Sender: TObject);
public
imgURL: array of string;
{ Public declarations }

end;

var
Form1: TForm1;
count_all, count: integer;

implementation

{$R *.fmx}

procedure TForm1.SpeedButton2Click(Sender: TObject);
var
i: integer;
begin

for i := 0 to Length(imgURL) - 1 do
begin

if (imgURL[i] <> '') then
begin

with TDownload.Create(False) do
begin

Sonuc := AnsiPos('.', imgURL[i]);
ID := AnsiLeftStr(imgURL[i], Sonuc - 1);
XIndex := AnsiMidStr(imgURL[i], Sonuc + 1, Length(imgURL[i]) + Sonuc);

URL := 'http://myapisite.com/upload/images/' + ID + '/1_small.jpg';
end;

end;
end;

end;

constructor ListThread.Create(Suspended: Boolean);
begin
Inherited Create(Suspended);
FreeOnTerminate := True;
end;

procedure ListThread.Execute;
var
http: TIdHTTP;
SObj1, SObj2: ISuperObject;
j: integer;
begin
http := TIdHTTP.Create;

SObj1 := TSuperObject.Create(http.Get('http://myapisite.com/?~/en/api/new/0/'
+ IntToStr(count) + '/10'));

count_all := StrToInt(SObj1.S['count_all']);

with SObj1.A['data1'] do
for j := 0 to Length - 1 do
begin
SObj2 := O[j];
begin

price := StrToInt(SObj2.S['price']);
name := StrToInt(SObj2.S['name']);
ID := StrToInt(SObj2.S['id']);
objCount := SObj2.count;
Synchronize(UpdateListThread);

end;
end;

end;

procedure ListThread.UpdateListThread;
var
LItem: TListViewItem;
begin

LItem := Form1.ListView1.Items.Add;
LItem.Height := 95;
LItem.Detail := name;
LItem.Text := price;
LItem.Bitmap := Form1.Image1.Bitmap;
SetLength(Form1.imgURL, objCount);
Form1.imgURL[LItem.Index] := IntToStr(ID) + '.' + IntToStr(LItem.Index);
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if (count_all > ListView1.Items.count) then
begin
count := count + 10;
with ListThread.Create(False) do
begin
OnTerminate := EndThread;
end;
AniIndicator1.Enabled := True;
AniIndicator1.Visible := True;
end;
end;

procedure TForm1.EndThread(Sender: TObject);
begin
AniIndicator1.Enabled := False;
AniIndicator1.Visible := False;
SpeedButton2.OnClick(Sender);
end;

Procedure TDownload.Execute;
var
http: TIdHTTP;
begin
http := TIdHTTP.Create;
MS := TMemoryStream.Create;
Bitmap := TImage.Create(nil);
try
http.Get(URL, MS);
Bitmap.Bitmap.LoadFromStream(MS);
Synchronize(UpdateDownloadThread);
finally
FreeAndNil(http);
FreeAndNil(MS);

end;

end;

procedure TDownload.UpdateDownloadThread;
begin
Form1.ListView1.Items.Item[StrToInt(XIndex)].Bitmap := Bitmap.Bitmap;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
count := 0;
with ListThread.Create(False) do
begin
OnTerminate := EndThread;
end;
AniIndicator1.Enabled := True;
AniIndicator1.Visible := True;

end;

end.



Thanks for your answers in advance.

Reply all
Reply to author
Forward
0 new messages