Перетаскивание Listview Kivy [дубликат]

Как будто вы пытаетесь получить доступ к объекту, который является null. Рассмотрим ниже пример:

TypeA objA;

. В это время вы только что объявили этот объект, но не инициализировали или не инициализировали. И всякий раз, когда вы пытаетесь получить доступ к каким-либо свойствам или методам в нем, он будет генерировать NullPointerException, что имеет смысл.

См. Также этот пример:

String a = null;
System.out.println(a.toString()); // NullPointerException will be thrown
0
задан Pistol Pete 25 May 2014 в 22:11
поделиться

1 ответ

Для тех, кто пытается перетащить из ListView, есть хорошая новость: существует решение. Чтобы решить эту проблему, я использовал методы, связанные с on_selection_change адаптера ListView, on_touch_down ListView и on_touch_up Scatter, которые я использовал для перетаскивания, как в следующем кодовый блок.

self.listview.bind(on_touch_down=self.press)
self.scatter.bind(on_touch_up=self.release)
self.adapter.bind(on_selection_change=self.selectionChange)

def selectionChange(self, adapter):
    if adapter.selection: #Sometimes the selection was [], so a check doesn't hurt 
        names = adapter.data
        self.scatter.children[0].text = adapter.selection[0].text #My scatter has a label as it's first and only child. Here, I'm changing the label's text
        for j in adapter.data:
            if j == adapter.selection[0].text:
                break
        names.pop(names.index(j))
        self.listview.adapter.data = names
        if(hasattr(self.listview, '_reset_spopulate')): #This is used to reset the ListView
            self.listview._reset_spopulate()

def press(self, view, touch):
    if view.collide_point(touch.x, touch.y) and not touch.is_mouse_scrolling:
        self.scatter.center = touch.pos
        self.floatLayout.add_widget(self.scatter) #The scatter appears on the click
        self.scatter.on_touch_down(touch) #Needs to be called to get the scatter to be dragged

def release(self, scatter, touch):
    if scatter.collide_point(touch.x, touch.y) and touch.grab_current: #Because Kivy's on_touch_up doesn't work like I think it does

        #Do whatever you want on the release of the scatter

        self.floatLayout.remove_widget(self.scatter) #Remove the scatter on release
1
ответ дан Pistol Pete 18 August 2018 в 07:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: