Cameron Sima
Cameron Sima

Reputation: 5385

Kivy: Dynamic pages which link to dynamically generated screens

I know it's possible to dynamically generate a list of widgets within a screen like this:

<ListViewModal>:
    ListView:
        size_hint: .8, .8
        adapter:
            sla.ListAdapter(
            data=["Item #{0}".format(i) for i in range(100)],
            cls=ListItemButton.ListItemButton)

but is it then possible to have each of these have a dynamic link (or binding, etc)?

I'm trying to have an interface like on Craigslist where a user selects their state from a scroll-through list, which opens a screen of that state's cities. Instead of making 50 buttons for each states and then 50 more screens each with a list of cities, I think it would be smarter to dynamically generate these from a list of states and a dictionary of states: cities.

I know I could do this with any given web framework, but is it feasible/possible in kivy?

EDIT:

I'm trying to implement an args_converter as suggested, but whenever I try to implement it like so:

class StatesScreen(Screen):
    data = [{'text': str(i), 'is_selected': False} for i in range(100)]

    args_converter = lambda row_index, rec: {'text': rec['text'],
                                             'size_hint_y': None,
                                             'height': 25}

    list_adapter = ListAdapter(data=data,
                               args_converter=args_converter,
                               cls= ListItemButton,
                               selection_mode='single',
                               allow_empty_selection=False)

nothing appears. And if I try to implement it in kv language,

<StatesScreen>
    ListView:
      adapter:
        list_adapter

I get list_adapter not defined

Upvotes: 0

Views: 303

Answers (1)

inclement
inclement

Reputation: 29488

You can use an args_converter function to convert the data format to a widget however you like. This is documented here.

Upvotes: 1

Related Questions