Adding KivyMD ListItem to MDList produces blank members after the first member

Viewed 64

I'm trying to populate an MDList in my KivyMD application with custom items that extend from BaseListItem. Here is my .kv code:

BoxLayout:
    canvas.before:
        Color:
            rgba: 0, 0, 0, 1
        Line:
            width: 2
            rectangle: self.x, self.y, self.width, self.height
            
    orientation: "vertical"
    size_hint: 1, 1
    pos_hint: {'center_x': 0.5}

    ScrollView:
        id: scrollableOutput
        scroll_y: 0 # auto scroll when adding content

        MDList:
            id: list_LynisResults

I need a custom list item that contains three items of text, one aligned to the left, one aligned to the center, and one aligned to the right of the rectangle. I inherited from OneLineListItem for the three pieces of text, which are called TypeItem, CodeItem, and MessageItem. These items are then added to a class that inherits from BaseListItem with add_widget().

class TypeItem(OneLineListItem):

    def __init__(self, **kwargs):
        super(TypeItem, self).__init__(**kwargs)
        self.text = "Suggestion"
        self.ids._lbl_primary.halign = "left"

class CodeItem(OneLineListItem):

    def __init__(self, **kwargs):
        super(CodeItem, self).__init__(**kwargs)
        self.text = "AUTH-9328"
        self.ids._lbl_primary.halign = "center"

class MessageItem(OneLineListItem):

    def __init__(self, **kwargs):
        super(MessageItem, self).__init__(**kwargs)
        self.size_hint_y=None

        self.ids._lbl_primary.halign = "right"

class LynisResultListItem(BaseListItem):

    lynisResult: LynisResult
    """Lynis result that is used to populate this list item."""

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)


    def setLynisResult(self) -> None:
        self.height = 60

        type = TypeItem()
        code = CodeItem()
        message = MessageItem()

        self.add_widget(type)
        self.add_widget(code)
        self.add_widget(message)

I get a reference to this MDList elsewhere in my code so I can call add_widget() on it:

self._list_LynisResults   = self.ids['list_LynisResults']
self._list_LynisResults.add_widget(LynisResultListItem())

When I call setLynisResult() to add a single entry, my list produces this, as expected (except for the black line below the MDList component):

enter image description here

Now, when I add multiple entries to the list, they are all blank, and it looks like their text has been added to the first entry instead:

enter image description here

Why is this happening? Is there another way to approach this problem of creating custom MDList components for the information I want to display?

0 Answers
Related