Search code examples
blackberryqmlblackberry-10cascadeblackberry-cascades

How can I deselect an item from ListView in cascade?


I'm developing a BlackBerry 10 mobile application using the Momentics IDE (native SDK).

I have a simple ListView and I want to change the background of a specific item when clicked according to the selection status. I thought th code below will work but it doesn't, any one can help one this ?

ListView{
        id: simpleList

        dataModel: groupDataModel

        listItemComponents: [
            ListItemComponent {
                type: "item"

                SQLvListItem {
                    id: containerItem

                    itemLabel: ListItemData.label

                    // Item Label Style
                    itemFontSize: ListItemData.fontSize
                    itemFontName: ListItemData.fontName
                    itemFontStyle: ListItemData.fontStyle
                    itemFontColor: containerItem.ListItem.selected ? ListItemData.fontColorSelected : ListItemData.fontColor

                    //Item background
                    containerBorderColor: ListItemData.borderColor
                    containerBackgroundColor: containerItem.ListItem.selected ? ListItemData.backgroundColorSelected : ListItemData.backgroundColor
                }
            }
        ]

        onTriggered: {
            var selectedItem = dataModel.data(indexPath);

            if (selected() == indexPath){
                select(indexPath, false);
                dataModel.itemUpdated(indexPath)
            }       
            else{
                select(indexPath, true);
            }
        }
    }

PS: the "SQLvListItem" is a custom item which I created


Solution

  • Actually it was so simple, you just need to use toggleSelection() function which toggles selection on an item ; if the item is selected, it becomes deselected and if the item is deselected, it becomes selected.

        ListView{
                id: simpleList
    
                dataModel: groupDataModel
    
                listItemComponents: [
                    ListItemComponent {
                        type: "item"
    
                        SQLvListItem {
                            id: containerItem
    
                            itemLabel: ListItemData.label
    
                            // Item Label Style
                            itemFontSize: ListItemData.fontSize
                            itemFontName: ListItemData.fontName
                            itemFontStyle: ListItemData.fontStyle
                            itemFontColor: containerItem.ListItem.selected ? ListItemData.fontColorSelected : ListItemData.fontColor
    
                            //Item background
                            containerBorderColor: ListItemData.borderColor
                            containerBackgroundColor: containerItem.ListItem.selected ? ListItemData.backgroundColorSelected : ListItemData.backgroundColor
                        }
                    }
                ]
    
                onTriggered: {
                    var selectedItem = dataModel.data(indexPath);    
                    toggleSelection(indexPath) // (solution 1)
    
                    // Or you can use also 
                    select(indexPath, (!isSelected(indexPath))) // (solution 2)
                }
            }