Refreshing GuiList (2 Viewers)

Dragy

Portal Pro
April 27, 2009
778
333
32
Home Country
Netherlands Netherlands
Hello,

I have made a plugin to retrieve a list of devices (lamps) in my home from a server, so I can turn them on / off. The plugin works, I can do what I want, but I only have to do some small improvements.

Now, I am refreshing the list of devices every x seconds, so the list gets updated so I can see which devices are on and off. (I use the item.IsPlayed for that, is that okay?). I have made this function for that:
Code:
    private void ListDevices()
    {
      GuiList.Clear();
 
      try
      {
        foreach (device d in devicesList.Values)
        {
          GUIListItem item = new GUIListItem();
          item.OnItemSelected += new MediaPortal.GUI.Library.GUIListItem.ItemSelectedHandler(listSelectDevice);
          item.Label = d.name;
          item.Label2 = d.type;
          item.ItemId = d.id;
          item.IsPlayed = d.status;
 
          GuiList.Add(item);
        }
        GUIPropertyManager.SetProperty("#itemcount", GuiList.Count.ToString());
        Log.Info("HCB: Devices listed");
 
        //set active type
        setActive(HCB.type.device);
      }
      catch
      {
        GUIPropertyManager.SetProperty("#hcb.status", "Listing devices failed!");
      }
    }

Bad thing about is that when you are not at the top of the list, the cursor jumps back to top after the list has refreshed. So I have to refresh the list without readding the items. How is that possible?

I thought about something like this:
Code:
    private void RefreshDevices()
    {
      foreach (device d in devicesList.Values)
      {
        GUIListItem item = new GUIListItem();
        item.OnItemSelected += new MediaPortal.GUI.Library.GUIListItem.ItemSelectedHandler(listSelectDevice);
        item.Label = d.name;
        item.Label2 = d.type;
        item.ItemId = d.id;
        item.IsPlayed = d.status;
      }
 
      GuiList.DoUpdate();
      Log.Info("HCB: Devices refreshed");
 
      GuiList.ReStorePosition();
    }
But unfortunately it's not working.. (Well yeah, I don't really understand how the GuiList works :()
Please, help me :)
 
Last edited:

Guzzi

Retired Team Member
  • Premium Supporter
  • August 20, 2007
    2,161
    747
    To get a defined item selected, remember the position and set it after you reloaded the list:
    GUIControl.SelectItemControl(GetID, (int)Controls.<listcontrol>, <indexposition>);
    But you don't need to clear and readd - you can just go through the existing list and update values:
    for (i = 0; i < this.<listcontrol>.Count; i++)
    { do updatess }
    AFAIK, enumeration is not possible, you need to loop with "for", not "foreach" ....
     

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    Thanks! I am a little further now. I still don't get how to assign the items to the list afterwards.. I now have this
    Code:
        private void RefreshDevices()
        {
          Log.Debug("HCB: refresh devices, devices: {0}, listed: {1}", devicesList.Count, GuiList.Count);
          for (int i = 0; i < GuiList.Count; i++)
          {
            device d = (device)devicesList.GetByIndex(i);
            Log.Debug("HCB: refresh device {0}", d.name);
    
            GUIListItem item = (GUIListItem)GuiList.Item(i)???;
            item.OnItemSelected += new MediaPortal.GUI.Library.GUIListItem.ItemSelectedHandler(listSelectDevice);
            item.Label = d.name;
            item.Label2 = d.type;
            item.ItemId = d.id;
            item.IsPlayed = d.status;
          }
          //GuiList.DoUpdate();
    
          Log.Info("HCB: Devices refreshed");
        }

    What should I change? I think there needs something to be done at the place where I placed the question marks.
     

    DJBlu

    Portal Pro
    August 14, 2007
    1,670
    813
    Llanelli
    Home Country
    United Kingdom United Kingdom
    Try this,
    Code:
     private void RefreshDevices()
    {
          Log.Debug("HCB: refresh devices, devices: {0}, listed: {1}", devicesList.Count, GuiList.Count);
          for (int i = 0; i < GuiList.Count; i++)
          {
            device d = (device)devicesList.GetByIndex(i);
            Log.Debug("HCB: refresh device {0}", d.name);
            (GUIListItem)GuiList.Item(i).Label = d.name;
            (GUIListItem)GuiList.Item(i).Label2 = d.type;
            (GUIListItem)GuiList.Item(i).ItemId = d.id;
            (GUIListItem)GuiList.Item(i).IsPlayed = d.status;
          }
          GuiList.DoUpdate();
          Log.Info("HCB: Devices refreshed");
    }
     

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    Nope, won't work:
    Error 1 'MediaPortal.GUI.Library.GUIListControl' does not contain a definition for 'Item' and no extension method 'Item' accepting a first argument of type 'MediaPortal.GUI.Library.GUIListControl' could be found (are you missing a using directive or an assembly reference?) D:\Documenten\Visual Studio 2010\Projects\MP_HomeControlBox\MP_HomeControlBox\HCB plugin.cs 305 30 MP_HomeControlBox
     

    DJBlu

    Portal Pro
    August 14, 2007
    1,670
    813
    Llanelli
    Home Country
    United Kingdom United Kingdom
    Just seen what I have put and its hoop.

    Try this.
    Code:
     private void RefreshDevices()
    {
          Log.Debug("HCB: refresh devices, devices: {0}, listed: {1}", devicesList.Count, GuiList.Count);
     
          foreach (GUIListItem item in GuiList)
          {
            device d = (device)devicesList.GetByIndex(item.ItemId);
            item.Label = d.name;
            item.Label2 = d.type;
            item.IsPlayed = d.status;
          }
          GuiList.DoUpdate();
          Log.Info("HCB: Devices refreshed");
    }
     

    Guzzi

    Retired Team Member
  • Premium Supporter
  • August 20, 2007
    2,161
    747
    Code:
    for (int i = 0; i < GuiList.Count; i++)
    {
    device d = (device)devicesList.GetByIndex(i);
    GuiList[i].Label = d.name;
    GuiList[i].Label2 = d.type;
    GuiList[i].ItemId = d.id;
    GuiList[i].IsPlayed = d.status;
    }

    Actually, if you only want to update the status, it should be enough to do:
    Code:
    for (int i = 0; i < GuiList.Count; i++)
    {
    device d = (device)devicesList.GetByIndex(i);
    GuiList[i].IsPlayed = d.status;
    }
     
    Last edited:

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    Both don't work, but I have solved it! The one of Guzzi gave me this idea:
    Code:
          for (int i = 0; i < GuiList.Count; i++)
          {
            device d = (device)devicesList.GetByIndex(i);
            Log.Debug("HCB: refresh device {0}", d.name);
            GuiList[i].IsPlayed = d.status;
          }
    
          GuiList.DoUpdate();

    It works :)

    DJBlu, it's not possible to enumerate GuiListItem.

    But thank you both!
     

    Guzzi

    Retired Team Member
  • Premium Supporter
  • August 20, 2007
    2,161
    747
    Code:
     private void RefreshDevices()
    {
          foreach (GUIListItem item in GuiList)
    [...]
    }

    as written above, GuiList is NOT enumerable - so you have to loop with "for" through the list...
     
    Last edited:

    Dragy

    Portal Pro
    April 27, 2009
    778
    333
    32
    Home Country
    Netherlands Netherlands
    By the way, I have some strange problem with the GUI Animations. All the windowopen and windowsclose animations don't work. I think it's because of my cocky code (I can't do better :p yet). Would you please take a look?

    Here is the whole plugin code: http://paste.ubuntu.com/1060534/

    There is something which blocks the animations.. but what?
     
    Last edited:

    Users who are viewing this thread

    Top Bottom