How to customize Listview at runtime?

Asked

Viewed 2,393 times

2

I’m having a problem with an example where I need to add a Text at the ListView which comes from an XML by code but does not appear when executed. Runs, but appears some warnings depreciation and the field does not appear. See below:

procedure TfrmUFC.LinkFillControlToField1FillingListItem(Sender: TObject;
  const AEditor: IBindListEditorItem);
var
   Item: TListViewItem;
   TextoIdade: TListItemText;
   TextField: TField;
begin
   Item := lvwLutadores.Items[AEditor.CurrentIndex];
   TextoIdade := Item.Objects.FindObject('Idade') as TListItemText;
   TextField := BindSourceDB1.DataSet.FindField('Idade');
   TextoIdade.Text := TextField.AsString;
end;

Warning:

[dcc32 Warning] uUFC.pas(48): W1000 Symbol 'FindObject' is deprecated: 'Use FindDrawable'

Configs of Text:

procedure TfrmUFC.lvwLutadoresUpdateObjects(const Sender: TObject;
  const AItem: TListViewItem);
var
  TextoIdade: TListItemText;
begin
  TextoIdade := TListItemText.Create(AItem);
  TextoIdade.Name := 'Idade';
  TextoIdade.Align := TListItemAlign.Trailing;
  TextoIdade.VertAlign := TListItemAlign.Center;
  TextoIdade.TextAlign := TTextAlign.Center;
  TextoIdade.PlaceOffset.X := -80;
  TextoIdade.PlaceOffset.Y := 0;
  TextoIdade.Font.Size := 13;
  TextoIdade.Width := 35;
  TextoIdade.Height := 18;
  TextoIdade.Visible := True;
end;

2 answers

0

Did he find the text area of the item? Try wearing something like:

ListView1.Items[0].Objects.TextObject.Text := BindSourceDB.DataSet.FieldByName('IDADE').AsString;

0

Philip,

Regarding the compiler’s Warning, "Finddrawable" is used when you already have the Text component on the screen and want to fetch it to change some property. Ex:

TListItemTextButton(Aitem.Objects.FindDrawable('Btn1')).Text = 'xxxx';

It’s not your case, you can ignore this Warning.

Looking at your code, the command below makes your object stay off the screen:

PlaceOffset.X := -80;

Try to use like this:

PlaceOffset.X := 0;

Another detail, you need to set the Text property to come out written something on the screen:

TextoIdade.Text := 'Agora Vai!';
  • No, how do I create an Itemobject.Age type item via code? Apparently you’re not creating. For example, there is Item, Detail but Age is not, and I want to create, receive this age and add it to Listview.

  • Filipe, to add a new field, do not use the "Updateobjects" event. You need to fire this from somewhere else, like a click on a button. Then you loop your listview and add the new Text. Ex: for i := 0 to listview.Items.Count - 1 of Begin txt := Tlistitemtext.Create(listview.Items[i]); txt.Text := 'abc'; ... end;

  • @Heber put this example in the answer. Without formatting for code it is difficult for the Filipe user to use this example correctly, if you want to emphasize it in the answer use [EDIT].

  • @Peace did not understand.. the code is formatted in the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.