0

Refresh of recycle view is not changed/updated on updating data from another class. I have seen multiple posts on this stackoverflow, but none of them are clear.

I need to refresh the recycle view every time, when the button 'nextscreen' is clicked.

Also provide a clear example to use the refresh_from_data() method and to which class it belongs to. Thanks in Advance.


import kivy
import sys
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen,SlideTransition,FadeTransition  
from kivy.properties import ObjectProperty, NumericProperty, BooleanProperty,ListProperty,StringProperty
from kivy.uix.button import Button
from kivy.base import runTouchApp
from kivy.uix.behaviors import FocusBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior


kv='''
ScreenManagement:
    id:screenmgr
    
    Screen1:
        id:scr1
        name:'scr1'
        manager:screenmgr
    Screen2:
        id:scr2
        name:'scr2'
        manager:screenmgr

<Screen1>:
    GridLayout:
        cols:3
        Label:
            text: "Enter any no."
        TextInput:
            multiline:False
            id:newno
        Button:
            text: 'Submit'
            on_press: root.getinput(newno.text)
        Button:
            text:'nextscreen'
            on_press:root.refreshview()
            on_release:root.manager.current = 'scr2'

<RecycleViewlist>:
    viewclass: 'SelectableLabel'
    SelectableRecycleBoxLayout:
        default_size: None, dp(56)
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        multiselect: True

<Screen2>:
    BoxLayout:
        listp:rv_list
        orientation:'vertical'
        Button:
            text:'Back'
            size:150,50
            on_press:app.root.current = 'scr1'
        Recycleviewlist:
            id: rv_list

'''

mylistno = 5
class Screen1(Screen):
    #newnumber =StringProperty()
    mylistnew = 0
    def getinput(self,number):
        self.mylistnew = int(number)
    def refreshview(self):
        if self.mylistnew != 0:
            print('refreshed new',self.mylistnew)
            rvobj.createview(self.mylistnew)
class Screen2(Screen):
    pass
class Recycleviewlist(RecycleView):
    def __init__(self,**kwargs):
        super(Recycleviewlist, self).__init__(**kwargs)
        self.data = [{'text': str(x)} for x in range(mylistno)]
    
    def createview(self,mylistnonew):
        self.data = [{'text': str(x)} for x in range(mylistnonew)]
rvobj = Recycleviewlist()

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)
    #sc3 = Screen3()

    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 Truej
        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


class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_string(kv)
        
class TestApp(App):
        
        def build(self):
                return presentation

if __name__ == '__main__':
    TestApp().run()                

1 Answers1

1

The line of code:

rvobj = Recycleviewlist()

creates a new instance of Recycleviewlist, but it is not the instance that appears in your GUI. So any changes you make to that instance will have no effect on your GUI. What you need to do is to get a reference to the Recycleviewlist that IS in your GUI, so try replacing refreshview() with:

def refreshview(self):
    # get a reference to the RecycleViewList
    rvobj = presentation.get_screen('scr2').ids.rv_list

    if self.mylistnew != 0:
        print('refreshed new', self.mylistnew)
        rvobj.createview(self.mylistnew)

and remove the line:

rvobj = Recycleviewlist()
John Anderson
  • 35,991
  • 4
  • 13
  • 36
  • firstly thanks very much, rvobj = presentation.get_screen('scr2').ids.rv_list ->ok this code captures the id of the recycleview. Is there any other method to get the above reference without using presentation ? Also please tell how to use the refresh_from_data() method? If possible provide a working example. – hari prasad Jul 22 '20 at 08:11
  • 1
    Using `presentation` is just a way to get the `root` of the GUI display (the `SceenManager`). Another way would be to use `App.get_running_app().root` instead of `presentation`. – John Anderson Jul 22 '20 at 12:56
  • You should rarely have to call `refresh_from_data()`, since it is supposed to get called automatically when the `data` changes. But if you find that the `RecycleView` is not updating, you can simply call `self.refresh_from_data()` on your `RecycleView`. – John Anderson Jul 22 '20 at 13:06