Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Listview.create - how to do it

Hi there

I have a listview on my form which I will update with alot of data with a procedure
I thought it would be much better to create a seperate listview which isn't shown to update and then assign the items back to the one the user can see - this saving alot of time since it doesn't have to draw them all for the user.

Anyway so I tried this

var
  lv : TListview;
begin
  lv.create(nil);
  For i := 0 to 10 do
  with lv.items.add do
     caption := IntToStr(l);

  listview1.items.assign(lv);
  lv.free;
end;

to test my theory however I get an access violation on lv.create(nil);
so I thought if I tried lv.create(Form1); however I still got an access violation...

any suggestions?
0
wildzero
Asked:
wildzero
  • 3
  • 2
1 Solution
 
2266180Commented:
yes.
1) that is because you didn't create the listview lv correctlty. correct way is:
var
  lv : TListview;
begin
  lv:=TListView.create(nil);// or form1

2) you assign a listview to the items property. correct way is

  listview1.assign(lv);
  lv.free;

but in your case the correct whole thing would be using tlistitems not a tlistview:
var
  li : TListItems; i:integer;
begin
  li := TListItems.create(listview1);// nil will not work
  For i := 0 to 10 do
  with li.add do
     caption := IntToStr(i);

//  listview1.items.assign(li);// no need for theese
//  li.free;
end;

as you can see, you problem is still not fixed.

the way these things are handled is:

  ListView1.Items.BeginUpdate;// disable automatic redraw
  ListView1.Items.clear;// lose the data if necessary
  for i:=1 to 10 do
    with ListView1.Items.add do
      caption:=inttostr(i);
  ListView1.Items.EndUpdate;// enable automatic redraw

cheers.
0
 
wildzeroAuthor Commented:
Thanks for that :)
The TListItems - does that contain subitems as well just like a Tlistview?

0
 
wildzeroAuthor Commented:
BeginUpdate and EndUpdate seems the way to go
Thanks :D
0
 
wildzeroAuthor Commented:
On a side note, is there a faster way of finding an item in a TListView other then doing

Var
  anItem:TListItem;

begin
  anItem:=listview.FindCaption(0, 'Caption', false, True, True);
  If AnItem <> nil then
     begin
       //Found
     
     end;

;-)
0
 
2266180Commented:
> The TListItems - does that contain subitems as well just like a Tlistview?
- the tlistitems contain a TStrings Items to hold the subitems.

regarding the searching: I don't know. but generally the delphi components (from the distribution) are usually pretty well optimized. of course, some of them are optimized for access speed, or for memory consumption, or for searching, or for other operations.

best way is to test: measure the time it takes for a few big searches with that method and then the same searches with other methods and you will know a more accurate result. though practise will always come back with an example that is not fast enough (after all it all depends on the algorithm and element to search for). there is a whole theory behind searching and sorting ;)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now