Problem displaying items in Listbox and Memo in Firemonkey Android application

  

I am at my first experiences with Android development using Delphi/Firemonkey 10.3.1 and have encountered the following problem with populating and displaying items in a TListbox component and a TMenu component at run time.

I have placed TListbox and TMenu components on a panel, together with a button to trigger the generation of a sequence of random numbers and display them in these components. The TListbox component contains objects of class TListboxItemRandomNo descending from TListboxItem. The code for this class is:

TListboxItemRandomNo= class(TListboxItem)
private
FRandomNo: integer; {Number displayed by Listbox item}
protected
procedure HandlePaint(Sender: TObject; Canvas: TCanvas; const ARect: TRectF);
function GetItemText: string;
public
constructor Create(Owner: TComponent; RandomNoValue: integer);
property RandomNo: integer read FRandomNo;
end;

constructor TListboxItemRandomNo.Create(Owner: TComponent;
RandomNoValue: integer);
var
Listbox: TListbox;
begin
Listbox:= Owner as TListbox;
inherited Create(Owner);
Listbox.AddObject(Self);
{Save RandonNo:}
FRandomNo:= RandomNoValue;
{Assign OnPaint event handler:}
OnPaint:= HandlePaint;
end;

procedure TListboxItemRandomNo.HandlePaint(Sender: TObject; Canvas: TCanvas;
const ARect: TRectF);
var
Text: string;
TextSize: TSizeF;
TextPosn: TPointF;
TextRect: TRectF;
begin
{Calculate item text:}
Text:= GetItemText;
{Set colour for text:}
Canvas.Fill.Color:= TAlphaColorRec.Black;
{Determine text image size, position and bounding rectangle:}
TextSize.cx:= Canvas.TextWidth(Text);
TextSize.cy:= Canvas.TextHeight(Text);
TextPosn.x:= ARect.Left + 5;
TextPosn.y:= (ARect.Top + ARect.Bottom – TextSize.cy)/2;
TextRect.TopLeft:= TextPosn;
TextRect.Right:= ARect.Right;
TextRect.Bottom:= TextRect.Top + TextSize.cy;
{Draw item text:}
Canvas.FillText(TextRect, Text, False, 1.0, [], TTextAlign.Leading, TTextAlign.Center);
end;

function TListboxItemRandomNo.GetItemText: string;
begin
Result:= IntToStr(Index) + ‘: ‘ + IntToStr(FRandomNo);
end;

Initially I put the code to generate and display the random numbers in the OnClick event handler of the button. Whilst this approach worked on the Windows 32-bit platform, on the Android platform in both the TListbox and the TMemo components the items were not displayed.

I then created an Action called ActionCalcRandomNos and moved the code to generate and display the random numbers from the button’s OnClick event handler into the OnExecute event handler of the action. I disconnected the OnClick event handler of the button and assigned ActionCalcRandomNos to it instead. After these changes I found that the items in both the TListbox and the TMemo components were displayed on both the Windows-32 and the Android platforms. The code in question is:

procedure TForm1.ActionCalcRandomNosExecute(Sender: TObject);
{Calculates a random sequence of numbers and displays them in TListbox
and TMemo components}
var
i: integer;
xi: integer;
ListboxItemRandomNo: TListboxItemRandomNo;
LineI: string;
begin
{Read number of random numbers required:}
FRandomNoCount:= StrToInt(EditItemCount.Text);
SetLength(FRandomNoSequence, FRandomNoCount);
{Generate random number sequence:}
Randomize;
for i := 0 to FRandomNoCount-1 do
begin
xi:= Random(FRandomNoCount);
FRandomNoSequence[i]:= xi;
end;
{Generate Listbox items dislaying random numbers:}
ListBoxRandomSequence.Clear;
ListBoxRandomSequence.BeginUpdate;
for i:= 0 to FRandomNoCount-1 do
begin
ListboxItemRandomNo:= TListboxItemRandomNo.Create(ListBoxRandomSequence,
FRandomNoSequence[i]);
end;
ListBoxRandomSequence.EndUpdate;
{Write random numbers to TMemo:}
MemoRandomNos.Lines.Clear;
MemoRandomNos.BeginUpdate;
for i:= 0 to FRandomNoCount-1 do
begin
LineI:= (ListBoxRandomSequence.ListItems[i] as TListboxItemRandomNo).GetItemText;
MemoRandomNos.Lines.Add(LineI);
end;
MemoRandomNos.EndUpdate;
end;

Why did this code not work when called directly from the button OnClick event handler, but did work when called indirectly via the action ActionCalcRandomNos connected to the button?

The only remaining minor problem with the TListbox is this. The item height, which is set as 0 in the Object Inspector, on the Android platform version only is being displayed much too great. This started after I changed the ItemHeight property back to 0 after previously having set it at 20.

Any light shed on any of these problems would be helpful.

Comments are closed.