我有一个简单的应用程序,它在 RecycleView 上显示来自 Sqlite 数据库的数据,并在选中后从 RecycleView 中显示行被删除; 我遇到的问题是,当删除一行时,选择会保留,当一个新的输入被传递到 RecycleView 时,它会自动被选中,因此被删除。
我希望在删除行后删除选择,而不必通过触摸输入取消选择它,这样新的输入就不会被删除。
这是我的代码示例:
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()