annlinx
annlinx

Reputation: 61

TListview FMX onclick event along with Toggle Design

Toggle Design mode in FMX Listview give you this

The implementation is the following:

ds.first;
  while not(ds.Eof) do
    begin
      L := LV1.Items.Add;
      L.Data['Part_No']   := ds.FieldByName('Part_no').AsString;
      L.Data['Part_Name'] := ds.FieldByName('Part_name').AsString;
      L.Data['LocNo']     := ds.FieldByName('Loc_No').AsString;
      L.Data['Qty']       := ds.FieldByName('BAL').AsFloat;
      ds.Next;
    end;

I add items dynamically using code

How can OnItemClick event fire to detect which element has been clicked. Very frustrating to find out just simple thing.

Any help appreciated.

Upvotes: 3

Views: 1710

Answers (3)

codeGood
codeGood

Reputation: 302

Use the OnItemClickEx event instead. This passes the ItemObject clicked, which you can then check the ItemObject.Name to see which element was clicked.

Upvotes: 1

skyzoframe
skyzoframe

Reputation: 11

"How can detect which element has been clicked or selected? Very frustrating to find out just simple thing."

1.- select structure like.: TlistView1/ItemAppearance/Item

2.- in object Inspector, change Appearance to DynamicAppearance.

3.- in object Inspector, add a new TTextObjectAppearance, change name to "MUV_HELY" or something.

4.- Add item

ListView1.Items.Add.Objects.FindObjectT<TListItemText>('MUV_HELY').Text:= //Your record value, or something. /*string all the time*/

/// With image ///

> ListView1.Items.Add.Objects.FindObjectT<TListItemImage>('IMAGE01').Bitmap := FMX.Graphics.TBitmap.CreateFromFile('.\Jelleg\List\'+Format('%.4d',[AdatSor.GYARTASISZAM])+'.bmp');

'.\' -> means your application Release folder. where your *.exe is. 'Format('%.4d',[AdatSor.GYARTASISZAM])' -> means for these example, in my database stored file name, what i have to call from file. My database value 1 and 2, but in file name I need to call 0001.bmp and 0002.bmp.

5.-Read back item

procedure TMainMenu.ListView1Change(Sender: TObject);
begin
  if ListView1.Selected<>nil then ShowMessage(TAppearanceListViewItem(ListView1.Selected).Objects.FindObjectT<TListItemText>('MUV_HELY').Text);
end;

If you change the selected item with keyboard (up, down, left, right), then you will get back the selected value, without clicking on it.

Upvotes: 0

Dave Nottage
Dave Nottage

Reputation: 3602

A bit thrown together, however it should have you started:

function GetClickedDrawable(const AItem: TListViewItem; APoint: TPointF): TListItemDrawable;
var
  I: Integer;
begin
  Result := nil;
  // Fudge for statusbar height if using iOS. Should be done properly
  APoint := PointF(APoint.X, APoint.Y - 20); 
  for I := 0 to AItem.Objects.ViewList.Count - 1 do
  begin
    if AItem.Objects.ViewList[I].InLocalRect(APoint) then
    begin
      Result := AItem.Objects.ViewList[I];
      Break;
    end;
  end;
end;

procedure TForm1.ListViewItemClick(const Sender: TObject; const AItem: TListViewItem);
var
  LDrawable: TListItemDrawable;
begin
  LDrawable := GetClickedDrawable(AItem, ListView.AbsoluteToLocal(Screen.MousePos));
  if LDrawable <> nil then
    ShowMessage(LDrawable.Name);
end;

You'll need to adjust GetClickedDrawable to suit whatever platform you're on. PS: Thanks for asking; I'm going to need to do something very much like this, eventually :-)

EDIT:

I've left my original answer in place in case the following is not available in earlier versions of Delphi:

Use the OnItemClickEx event. The parameters of the event make it pretty self explanatory

Don't know why I didn't see that earlier :-)

Upvotes: 2

Related Questions