Jump to content

Dynamic create menu items to TfgListMenu


MoreWood

Recommended Posts

 Hello, I've two app options that change the menu items displayed on TfgListMenu, and I clear the list when app option change and create - add (3 or 6 menuitems), when I do the change from 6 to 2 I get this error message: "The current value [5] is not in range [0,2]", I imagine that is something related to item selected, but I don't know how to fix it, thanks.

 

Link to comment
Share on other sites

Hello Yaroslav, I've download the 1.4.3.0 version and it seems the problem persists, here is the code I use to create the menu options:

procedure TFormMain.LoadMenus;
var
 MenuItem: TfgListMenuItem;
begin
 fgMainMenu.BeginUpdate;
 fgMainMenu.Index:= 0; //added this trying to fix the problem
 fgMainMenu.Items.ClearAndResetID;

 if TConfig.Loged then
  begin
   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Entradas';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.ENTRA;
   MenuItem.OnTap:= MenuEntra;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Salidas';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.SALE;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Stock';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.STOCK;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Datos palet';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.INFO;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= '';
   MenuItem.Style:= TfgMenuItemStyle.Divider;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Ajustes';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.AJUSTES;
   MenuItem.OnTap:= MenuAjustes;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Salir';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.SALIR;
   MenuItem.OnTap:= MenuLogout;
  end else
  begin
   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Validar';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.LOGIN;
   MenuItem.OnTap:= MenuLogin;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= '';
   MenuItem.Style:= TfgMenuItemStyle.Divider;

   MenuItem:= fgMainMenu.Items.Add as TfgListMenuItem;
   MenuItem.Title:= 'Ajustes';
   MenuItem.Style:= TfgMenuItemStyle.PrimaryItem;
   MenuItem.IconName:= R.Bitmap.AJUSTES;
   MenuItem.OnTap:= MenuAjustes;
  end;

 fgMainMenu.EndUpdate;
end;

Thank you.

Link to comment
Share on other sites

Hello Yaroslav, I've done a small program with the same code to generate the menu options and it works, but the original continues with the same issue. This is the log, I don't know it it will help you :

 

2020-08-10 14:07:49.542 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="mainDrawer", class="TfgDrawer", duration="2 msec"
2020-08-10 14:07:49.546 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2]. 
2020-08-10 14:07:49.628 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2].  
2020-08-10 14:07:49.628 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2].  
2020-08-10 14:07:49.666 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:07:49.671 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 5 lines
2020-08-10 14:07:49.673 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:07:49.673 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:07:49.677 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 1 line
2020-08-10 14:07:49.679 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:11.352 2003-2070/mpl.kt.keeptrack D/OpenGLRenderer: endAllActiveAnimators on 0xcca3e780 (RippleDrawable) with handle 0xccc837c0
2020-08-10 14:08:11.373 2003-2003/mpl.kt.keeptrack A/fatal: [Java bridge]: cannot remove JNIObject. Delphi listener object are not registered. instance=CCAA6220
2020-08-10 14:08:21.440 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgButton", duration="0 msec"
2020-08-10 14:08:21.446 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgButton", duration="0 msec"
2020-08-10 14:08:21.449 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgLabel", duration="0 msec"
2020-08-10 14:08:21.460 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgEdit", duration="0 msec"
2020-08-10 14:08:21.462 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgLabel", duration="0 msec"
2020-08-10 14:08:21.467 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgEdit", duration="0 msec"
2020-08-10 14:08:21.468 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: posted request on realign: root="FLogin: TFLogin", initiator="FLogin: TFLogin"
2020-08-10 14:08:21.482 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="FLogin", class="TFLogin", duration="1 msec"
2020-08-10 14:08:21.483 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: posted request on realign: root="FLogin: TFLogin", initiator="FLogin: TFLogin"
2020-08-10 14:08:21.489 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: posted request on realign: root=": TfgMainContent", initiator="FLogin: TFLogin"
2020-08-10 14:08:21.490 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgMainContent", duration="1 msec"
2020-08-10 14:08:21.491 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgMainContent", duration="1 msec"
2020-08-10 14:08:21.509 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:21.510 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 1 line
2020-08-10 14:08:21.511 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:21.511 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:21.513 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 1 line
2020-08-10 14:08:21.514 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:22.744 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="mainDrawer", class="TfgDrawer", duration="0 msec"
2020-08-10 14:08:22.745 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="", class="TfgMainContent", duration="1 msec"
2020-08-10 14:08:22.759 2003-2003/mpl.kt.keeptrack A/fatal: [Java bridge]: cannot remove JNIObject. Delphi listener object are not registered. instance=CCAA6A18
2020-08-10 14:08:22.765 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:22.766 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 1 line
2020-08-10 14:08:22.767 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:22.767 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:22.777 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 4 lines
2020-08-10 14:08:22.778 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:22.780 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onCreateViewHolder
2020-08-10 14:08:22.786 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:29.927 2003-2003/mpl.kt.keeptrack D/debug: [FlexBox]: recalculated Layout: name="mainDrawer", class="TfgDrawer", duration="1 msec"
2020-08-10 14:08:29.929 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2]. 
2020-08-10 14:08:29.930 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2].  
2020-08-10 14:08:29.930 2003-2003/mpl.kt.keeptrack E/error: The current value [6] is not in range [0, 2].  
2020-08-10 14:08:29.942 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:29.947 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 5 lines
2020-08-10 14:08:29.948 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onViewRecycled
2020-08-10 14:08:29.948 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:29.952 2003-2003/mpl.kt.keeptrack I/chatty: uid=10226(mpl.kt.keeptrack) identical 1 line
2020-08-10 14:08:29.954 2003-2003/mpl.kt.keeptrack D/NativeListViewAdapter: onBindViewHolder
2020-08-10 14:08:34.521 2003-2070/mpl.kt.keeptrack D/OpenGLRenderer: endAllActiveAnimators on 0xccd04f80 (RippleDrawable) with handle 0xccc83850
2020-08-10 14:08:34.539 2003-2003/mpl.kt.keeptrack A/fatal: [Java bridge]: cannot remove JNIObject. Delphi listener object are not registered. instance=CCAA5848

 

Link to comment
Share on other sites

Hello the error raise in this line from th Select procedure (I notice that this method is called twice when I click a menu option, maybe some check to avoid it could be interesting):

procedure TfgListMenuSelectionController.Select(const AItemIndex: Integer);
var
  Item: TfgListMenuItem;
begin
  TfgAssert.IsNotNil(FSelection, 'FSelection');
  TfgAssert.IsNotNil(FListMenu, 'FListMenu');
  TfgAssert.InRange(AItemIndex, 0, FListMenu.Items.Count - 1); <--  HERE ERROR IS RAISED ¡

  if Mode = TfgListMenuSelectionMode.None then
    Exit;

  BeginUpdate;
  try
    Item := FListMenu.Items.ByIndex[AItemIndex];
    if Item.Selectable and not FSelection.Contains(Item) then
    begin
      if Mode = TfgListMenuSelectionMode.SingleSelect then
        ResetSelection;

      Item.IsSelected := True;
      FSelection.Add(Item);
      FIsSelectionChanged := True;
    end;
  finally
    EndUpdate;
  end;
end;

 Also it seems that fgMainMenu.Index:= 0; don't help to change the current selected item. The small demo I say before it works fine was because the create menus procedure was called from a button, if I call the procedure from a menu option the error is raised, I think I understand this error: maybe the sequence:

- tap menu option

- call OnTap menu option --> create menu items

- Select procedure is called but with a wrong index (6 in this case).

cause the problem, so Select should occur before OnTap, or the Items.Clear should reset the index to 0 (or unselectd -1). Other solutions could be no calling Select procedure, because I don't do it . Attached the small demo, iy fou push the button it works, if you push menu item item (login or logout options) it falls when change from 6 6 to 3 menu options.

Thanks

CreateMenu.rar

  • Like 1
Link to comment
Share on other sites

2 minutes ago, Yaroslav Brovin said:

Great, thank you.

I will have a look later today and prepare hotfix release.

Thank you 

  By the moment I can wait, so I don't need an inmediatly hotfix, so for me is not urgent, maybe wait and check other things.

Link to comment
Share on other sites

  • Administrators

Ok, finally I have looked at your demo sample. And there are issue in your demo.

Problem

You remove list items from the Item's event handler. So other words it means to destroy menu item from himself. It's not a right pattern.

Solution

You should use a delayed rebuilding process. To do this wrap your rebuild procedure into TThread.ForceQueue. It will invoke rebuilding your menu outside of menu. And everything will be ok.

 TThread.ForceQueue(nil, procedure begin
   LoadMenus;
 end);

 

  • Thanks 1
Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...