Firemonkey TListView Delete Last Item on List Item Swipe Crash

  

I am using dephi firemonkey on Rad Studio 10.3.2 testing on android 9

I Want to Delete the last Item From a TListView. But Before I delete it I Want to ask a confirmation, then delete.

for that I built the sample code below, it has 1 TListview, 2 Speedbuttons, 1 rectangle and 1 label.

the rectangle is visible false, so when the user swipe the listviewitem it will show the delete button. in the delete button i will cancel delete and put the rectangle visible whith the question, if click yes then delete the item. the problem is that the listviewitem delete button never go away and when the user clicks the screen again the app crash.

the following images illustrate the operation

Before swipe

After swipe

after click delete

after click yes

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.ListView.Types, FMX.ListView.Appearances, FMX.ListView.Adapters.Base,
FMX.ListView, FMX.StdCtrls, FMX.Controls.Presentation, FMX.Objects;

type
TForm1 = class(TForm)
ListView1: TListView;
Rectangle1: TRectangle;
SpeedButton1: TSpeedButton;
SpeedButton2: TSpeedButton;
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure ListView1DeletingItem(Sender: TObject; AIndex: Integer;
var ACanDelete: Boolean);
procedure SpeedButton1Click(Sender: TObject);
procedure SpeedButton2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
ItemDelete : Integer;

implementation

{$R *.fmx}

procedure TForm1.FormCreate(Sender: TObject);
var
Item: TListViewItem;
begin
Item := ListView1.Items.Add();
Item.Text := ‘Item 1’;
Item := ListView1.Items.Add();
Item.Text := ‘Item 2’;
Item := ListView1.Items.Add();
Item.Text := ‘Item 3’;
Item := ListView1.Items.Add();
Item.Text := ‘Item 4’;
end;

procedure TForm1.ListView1DeletingItem(Sender: TObject; AIndex: Integer; var ACanDelete: Boolean);
begin
ACanDelete := false;
ItemDelete := AIndex;
Rectangle1.Visible := true;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
ItemDelete := -1;
Rectangle1.Visible := false;
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
ListView1.Items.Delete(ItemDelete);
ItemDelete := -1;
Rectangle1.Visible := false;
end;

end.

Comments are closed.