Tlistview Delphi

时间:2019-04-11 06:56:38

标签: listview delphi

我有一个包含一些数据的ListView,它工作正常,除了当我试图从最后一行获取数据时,在这种情况下,它返回随机数据。

我有一个函数,将对象(来自一个类的对象列表)添加到列表视图,然后在另一个对象上删除数据库中的数据并再次填充列表视图。

///我在这里填充列表视图

procedure TFrameEinExpression.Fill_Rows;
var
  itm : TFormulaFeatures;
  i : integer;
begin
  lvModels.Clear;
   EinExpression.LoadFeatureList;

  if (not Assigned(EinExpression)) or (EinExpression.FeatureList.Count = 0)  then
    Exit;
  lvModels.Items.BeginUpdate;

  try
    for i := 0 to EinExpression.FeatureList.Count - 1  do
      with lvModels.Items.Add do
        begin
          itm := TFormulaFeatures(EinExpression.FeatureList.Items[i]);
          Data := TFormulaFeatures(EinExpression.FeatureList.Items[i]);
          Caption := IntToStr(itm.Posicion);
          SubItems.Add(BoolToStr(itm.Presencia, True));
          SubItems.Add(itm.Descripcion);
          SubItems.Add(IntToStr(itm.Idcaract1));
        end;
  finally
    lvModels.Items.EndUpdate;
    if Assigned(itm) then
      itm.Free;
  end;
end;



//Here I perform the action (it works fine except for the last row)


if (Assigned (lvModels.Selected)) and (Assigned (lvModels.Selected.Data)) then

//This already doesn't have anything inside
showmessage('Desc :'+ TFormulaFeatures(lvModels.Selected.Data).Descripcion);

  begin
    BDMultiEP.SQLExecute('DELETE from [EXPRESSION] WHERE (idcaract=:Z) AND 
    (idcaract1=:Y)', [EinExpression.IDCARACT,
     TFormulaFeatures(lvModels.Selected.Data).IDCARACT1]);
  end;


//after this i take the values from the DB to the class and populate the List View again.
LoadFeatureList;
Fill_Rows;

在showmessage(用于测试)中,我有字段描述,但是最后一个是什么时候我什么都没有。

1 个答案:

答案 0 :(得分:4)

此行if Assigned(itm) then itm.Free;释放了EinExpression.FeatureList的最后一项。

由于itm对该对象的生存期不承担任何责任,因此这是最可能导致最后一个列表项中数据丢失的原因。

删除该行。

相关问题