'How can I deselect a selected row in Kivy Recycleview
I have a simple application that displays data from Sqlite database on RecycleView and Rows from RecycleView once selected get deleted; the problem I'm having is that When a row is deleted the selection stays and when a new Input gets passed into The RecycleView it gets automatically selected therefore deleted.
I want the selection to be removed once the row is deleted without having to deselect it with touch input so that new Inputs don't get deleted.
Here is an example of my code:
class DataDisplay(RecycleView, BoxLayout):
def _init_(self,**kwargs):
super(DataDisplay, self)._init_(**kwargs)
self.updateView()
def updateView(self):
conn = sqlite3.connect('Shop.db')
c = conn.cursor()
records = c.execute("SELECT rowid, product, quantity, price FROM INVENTORY")
#print([item for item in records])
self.data = [{'text':str(str(record[0]) + ')' + str(record[2]) + ' Quantite de ' + str(record[1]) + ' est ' + str(record[3]))} for record in records]
def remove_selection(self):
for child in self.children[0].children:
if child.selected:
child.selected = False
class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior,
RecycleBoxLayout):
touch_deselect_last = BooleanProperty(True)
''' 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)
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
get_rowid = rv.data[index]['text']
search_key = get_rowid.split(')')[0]
if is_selected:
conn = sqlite3.connect('Shop.db')
c = conn.cursor()
c.execute("DELETE FROM INVENTORY WHERE rowid = '{}'".format(int(search_key)))
conn.commit()
conn.close()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
