Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Adding a TOBjectList<Generic> to a TListView


This question is answered.


Permlink Replies: 1 - Last Post: Jun 4, 2014 2:00 PM Last Post By: Remy Lebeau (Te...
LAURENCE KILLEN

Posts: 41
Registered: 2/20/00
Adding a TOBjectList<Generic> to a TListView  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 4, 2014 1:22 PM
I have a generic TObjectList that I assign several hundred instances of an xBoxData.
TxBoxData = class
Address1: string;
RowID: String;
ColID: String;
xCoord: String;
yCoord: String;
Lat: string;
Long: String;
City: String;
State: String;
xBoxPath: String;
end;

I populate the xBoxData record then I assign is to a TObjectList.

procedure TfrMain.LoadFileLst(Sender: TObject);
var
SelectedFile: string;
xBoxFile: TextFile;
buffer: string;
const
XBOXDELIM = '[NEXT]';
begin
ListOfXboxData.Clear;
SelectedFile := ListBox1.Items.Strings[ListBoxItemIndex];
begin
AssignFile(xBoxFile, SelectedFile);
Reset(xBoxFile);
while not EOF(xBoxFile) do
begin
BoxData := TxBoxData.Create;
buffer := '';
ReadLn(xBoxFile, buffer);
if buffer = XBOXDELIM then
Begin
ReadLn(xBoxFile);
ReadLn(xBoxFile, BoxData.Address1);
ReadLn(xBoxFile, buffer); // Row & Col
BoxData.RowID := copy(buffer, 0, 5);
BoxData.ColID := copy(buffer, 5, 10);
ReadLn(xBoxFile, buffer); // xy Coords
BoxData.xCoord := copy(buffer, 0, pos(' ', buffer));
BoxData.yCoord := copy(buffer, pos(' ', buffer), Length(buffer));
BoxData.xBoxPath := copy(SelectedFile, 0,
(pos('\data', SelectedFile) - 1));
BoxData.City := edtCity.Text;
BoxData.State := edtState.Text;
End;
ListOfXboxData.Add(BoxData);
end;
CloseFile(xBoxFile);
end;
savedFlag := false;
btnSaveXBox.Enabled := not(savedFlag);
DisplayList; -->> displays the addreses in a memo box.
end;

So far, I have been displaying the data in a memo box and allowing the user to edit it there. I then write the results back to the TObjectList. It works but is inefficient and sloppy. I only send the Street addresses to the memo box and I allow them to delete Addresses, which makes it complicates to manage when I have to determine which xBoxData records to delete.

The bottom line is that I would like to see the Address field of my TobjectList in a TListView component and allow the use to directly manipulate the data there. Is TListView the right component? Is LiveBinding necessary? I have never done it.
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Adding a TOBjectList<Generic> to a TListView
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 4, 2014 2:00 PM   in response to: LAURENCE KILLEN in response to: LAURENCE KILLEN
Springer wrote:

The bottom line is that I would like to see the Address field of my
TobjectList in a TListView component and allow the use to directly
manipulate the data there. Is TListView the right component?

Yes, and no. A TListView would be very good for displaying your Box data
in a clean, organized manner, especially if you use it in virtual mode (set
its OwnerData property to True and assign an OnData event handler), eg:

procedure TfrMain.LoadFileLst(Sender: TObject);
var
  SelectedFile: string;
  xBoxFile: TextFile;
  buffer: string;
const
  XBOXDELIM = '[NEXT]';
begin
  ListView1.Items.Count := 0;
  ListOfXboxData.Clear; // make sure OwnsObjects is True
 
  SelectedFile := ListBox1.Items.Strings[ListBoxItemIndex];
  AssignFile(xBoxFile, SelectedFile);
  try
    Reset(xBoxFile);
    while not EOF(xBoxFile) do
    begin
      BoxData := TxBoxData.Create;
      try
        buffer := '';
        ReadLn(xBoxFile, buffer);
        if buffer = XBOXDELIM then
        begin
          ReadLn(xBoxFile);
          ReadLn(xBoxFile, BoxData.Address1);
          ReadLn(xBoxFile, buffer); // Row & Col
          BoxData.RowID := copy(buffer, 0, 5);
          BoxData.ColID := copy(buffer, 5, 10);
          ReadLn(xBoxFile, buffer); // xy Coords
          BoxData.xCoord := copy(buffer, 0, pos(' ', buffer));
          BoxData.yCoord := copy(buffer, pos(' ', buffer), Length(buffer));
          BoxData.xBoxPath := copy(SelectedFile, 0, (pos('\data', SelectedFile) 
- 1));
          BoxData.City := edtCity.Text;
          BoxData.State := edtState.Text;
        end;
        ListOfXboxData.Add(BoxData);
      except
        BoxData.Free;
        raise;
      end;
    end;
  finally
    CloseFile(xBoxFile);
  end;
  savedFlag := false;
  btnSaveXBox.Enabled := True;
  ListView1.Items.Count := ListOfXBoxData.Count;
end;
 
procedure TfrMain.ListView1Data(Sender: TObject; Item: TListItem);
var
  BoxData: TxBoxData;
begin
  BoxData := ListOfXboxData.Items[Item.Index];
  Item.Data := BoxData;
  // populate Item.Caption and Item.SubItems as needed...
end;


However, for editing the data, TListView is not well-suited for that (it
CAN be done, but it takes a lot of work to manipulate the ListView's built-in
editor, which is designed for editing only the 1st column). You should provide
a separate UI for editing data, whther that be a edit fields, or a separate
dialog, that is up to you to decide. You can use the TListView.Selected
property to know which list item is selected, and use the TListItem.Data
property to access the TxBoxData object for that list item. You can then
manipulate the TxBoxData member values as needed, and call TListView.UpdateItems()
to trigger a redraw for the list item, which will trigger the TListView.OnData
event so you can update the TListItem with the new data.

Is LiveBinding necessary?

Necessary, no. Usable, I don't know. I have not worked with LiveBindings
yet.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02