我有以下数据:http://qs.quantumsoftware.co.za/rust/items.json
我想要实现的是将每个项目设置在其特定的标题下,并动态地进行所有操作。我前阵子这样做了,不记得我是怎么做到的。
基本上,displayName
下的category
项具有列表视图项和标题。
编辑:我忘了提到类别字段中的文本也是动态的,因此我不知道类别字段中的文本使搜索变得困难或不可能。
答案 0 :(得分:0)
好像我明白了。我需要将所有类别加载到数组中,然后创建标题,然后仅在标题与类别字段匹配时添加一个项目。 代码:
procedure TDownloadItems.Execute;
var
jdata, jcategories: TdJSON;
http: TIdHTTP;
LItem: TListViewItem;
sCategories: TStringList;
I: Integer;
arrCategories: array of string;
begin
SetLength(arrCategories, 0);
http := TIdHTTP.Create(nil);
try
jdata := TdJSON.Parse(http.Get('http://qs.quantumsoftware.co.za/rust/items.json'));
try
sCategories := TStringList.Create;
try
sCategories.Sorted := True;
sCategories.Duplicates := dupIgnore;
for jcategories in jdata do
begin
sCategories.Add(jcategories['category'].AsString);
end;
SetLength(arrCategories, sCategories.Count);
for I := 0 to sCategories.Count - 1 do
arrCategories[I] := sCategories[I]
finally
sCategories.Free;
end;
Synchronize(
procedure
var
acategory: string;
jItems: TdJSON;
begin
frmMain.lvRustItems.BeginUpdate;
for acategory in arrCategories do
begin
with frmMain.lvRustItems.Items.Add do
begin
Text := acategory;
Purpose := TListItemPurpose.Header;
end;
for jItems in jdata do
begin
if jItems['category'].AsString = acategory then
begin
LItem := frmMain.lvRustItems.Items.Add;
LItem.Text := jItems['displayName'].AsString;
LItem.Data[TMultiDetailAppearanceNames.Detail1] := jItems['name'].AsString;
LItem.Data[TMultiDetailAppearanceNames.Detail2] := 'Rarity: ' + jItems['rarity'].AsString;
LItem.Data[TMultiDetailAppearanceNames.Detail3] := 'Stackable: ' + jItems['stackable'].AsString;
end;
end;
end;
frmMain.lvRustItems.EndUpdate;
end);
finally
jdata.Free;
end;
finally
http.Free;
end;
end;