Как динамически загружать данные с помощью RecycleView в KIVY, python - PullRequest
1 голос
/ 26 марта 2019

Я пытаюсь добавить данные в базу данных sqlite3, чтобы динамически читать, добавлять и удалять их из RecycleView, но я не могу заставить их автоматически загружаться после загрузки моих данных в базу данных.

Это часть моего кода, которую мне нужно изменить, чтобы он работал.Большое спасибо.

producto=[]

def cargarproductosbase():
    global producto
    con = sqlite3.connect("base.db")
    cur = con.cursor()
    cur.execute("select nombre from producto")
    print("se cargaron los productos")
    rows = cur.fetchall()
    for row in rows:
        for i in row:
            producto.append(i)
            print(producto)
    con.commit()
    con.close()
    print ("cargue estos datos")
    print (producto)

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
                                 RecycleBoxLayout):
    ''' Adds selection and focus behaviour to the view. '''


class SelectableLabel(RecycleDataViewBehavior, Label):
    ''' Add selection support to the Label '''
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)
    cols=2

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(SelectableLabel, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(SelectableLabel, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            aceptar=AceptarDonacion()
            aceptar.open()

            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))


class ExampleRV(RecycleView):
    global producto
    cargarproductosbase()
    def __init__(self, **kwargs):
        global producto

        super(ExampleRV, self).__init__(**kwargs)

        Clock.schedule_interval(self.reloading, 1)

    def reloading(self, nothing):
        global producto
        self.data = [{'text': str(x)} for x in producto]
        print (producto)

1 Ответ

0 голосов
/ 26 марта 2019

Экземпляр класса cargarproductosbase() в конструкторе класса ExampleRV.

Отрывки

class ExampleRV(RecycleView):

    def __init__(self, **kwargs):
        global producto
        super(ExampleRV, self).__init__(**kwargs)
        cargarproductosbase()
        self.data = [{'text': str(x)} for x in producto]
        print(producto)

выход

RV - data loaded

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...