+ self.current_index = None
+ self.items_cache = []
+ self.set_view(appuifw.Listbox([], self.entry_selected))
+ self.view.bind(EKeyUpArrow,lambda: self.arrow_key_pressed(-1))
+ self.view.bind(EKeyDownArrow,lambda: self.arrow_key_pressed(1))
+
+ def arrow_key_pressed(self, dir):
+ """
+ This function is called when an arrow key is pressed. Since we
+ don't get any "current list index has changed" events, we'll
+ have to create these ourselves this way.
+
+ Since the current index is only updated after the key event,
+ we'll have to adjust the index with the direction of the
+ keypress (-1 for up, +1 for down).
+ """
+ self.current_index = self.wrap_index(self.selected_index() + dir)
+ self.index_changed()
+ self.current_index = None
+
+ def refresh(self):
+ self.refresh_list()
+ super(ListView, self).refresh()
+
+ def refresh_list(self):
+ """ Reload the list items. Calls items() again. """
+ # Remember which item was selected
+ selected = self.selected_item()
+ # Refresh the list
+ self.items_cache = self.items()
+ try:
+ # Try to find the selected item in the new list (based on
+ # the display text).
+ selected_index = self.items_cache.index(selected)
+ except ValueError:
+ # If the selected item is no longer present, just keep the
+ # index the same (but be careful not to fall off the end).
+ selected_index = self.clip_index(self.selected_index())
+ # Update the items in the view
+ self.view.set_list(self.items_cache, selected_index)
+
+ def run(self):
+ self.index_changed()
+ super(ListView, self).run()