Kivy recycleview duplicating data items

Viewed 116

I have a recycle view in kivy im writing to it with a list containing text and images, the text is fine but as soon as i write an image its duplicating the image in the view as i scroll down please see code below, how would i stop this from happening, here is my kv file I have a boxlayout with two sets of data, heres a picture of the problem as wellexample

kv file:

 <SelectableReportTextbox@BoxLayout>:


    text: ''
    imagesource: ''
    color: ''


    Button:
            size_hint:  .8, 1
            text_size : self.text_size
            size_hint_y: None
            foreground_color: [1, 1, 1, 1]
            background_color: (0.2, 0.4, 0.8, 0.9) # root.color
            font_name: 'C:\kivy_venv\Graphics\GIL_____.TTF'
            font_size: self.height*0.2
            background_normal: root.imagesource
            text: root.text





  ScreenTwo:

    rv2: rv2


    canvas.before:


            Rectangle:
                    size:self.size           #100, 100
                    pos: self.pos
                    source: "C:\kivy_venv\Graphics\Jetfireback.png" 



    RecycleView_B:


            bar_width: 6
            size_hint: (None, None)
            do_scroll_y: True
            id: scrlv2
            size: (500, 500)
            pos_hint: {'center_x': .75, 'center_y': .64}
            multiline:True



            ProjectRV:


                    viewclass: 'SelectableReportTextbox'        #  
                    orientation: "horizontal"
                    scroll_type: ['bars', 'content']
                    scroll_wheel_distance: dp(114)
                    key_size: "height"

                    padding:1, 1
                    space_x: self.size[0]/3
                    id: rv2
                    pos_hint: {'center_x': 0.32, 'center_y': 0.525}
                    bar_width: dp(25)
                    bar_color: (0.7, 0.1, 0.3, 0.7)
                    bar_inactive_color: (0.1, 0.1, 0.1 , 1)
                    scroll_y : 0



                    SelectableRecycleBoxLayout:
                            rv2:rv2

                            spacing : '6'
                            default_size_hint: 1, None
                            size_hint_y: None
                            size_hint_x: 1

                            height: self.minimum_height
                            multiselect: True
                            touch_multiselect: True
                            orientation: 'vertical'

main.py:

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



 class SelectableReportTextbox(RecycleDataViewBehavior):
        selectable = BooleanProperty(True)
        index =None

        selected = BooleanProperty(False)
def __init__(self, **kw):
    super().__init__(**kw)
def refresh_view_attrs(self, rv, index, data):

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


class ProjectRV(RecycleView):


def __init__(self, **kwargs):
    super(ProjectRV, self).__init__(**kwargs)




 class RecycleView_B(RecycleView):

      pass

  class ScreenTwo(Screen):

        TEXT_FILE = open("output.txt", "a")

        zdata = re.search('{.*,', strdata).group(0)   
        splitex = zdata.split(':' + "'")[1]

        print (zdata)

        TEXT_FILE.write(zdata)

        TEXT_FILE.close()

        with open("output.txt", "r") as f:

           txtdata = eval(str('[' + ''.join(f.readlines()) + ']'))#ast.literal_eval

           nstring = type(txtdata)

           self.rv2.data = txtdata
           self.rv2.refresh_from_viewport()
           self.rv2.refresh_from_data()
1 Answers

The RecycleView re-uses viewclass object instances, and sets properties of those instances based on the data dictionaries. So, if one of your dictionaries sets the imagesource property of a viewclsas instance and then that instance is re-used for a dictionary that does not set the imagesource property, then that instance will retain that prior value of imagesource. The fix is to make sure that every dictionary in the data contains a value for every property of the viewclass. Each dictionary needs to include values for text, imagesource, and color, even if the value is ''.

Related