Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Cascades Development

Reply
Developer
billbsb
Posts: 72
Registered: ‎03-25-2011
My Carrier: None

Initialize ListView with selected item

Hello guys,

 

My app has a ListView and it's only shown after its dataSource is retrieved from the internet. The user is able to select only one item of the list at a time. I'm using a custom ListItemComponent that changes the background color if it's selected.

 

Everything all the UI is implemented in QML and only the data fetching is done in C++ and everything works just fine.

 

But now I want to set a selected item automatically once the ListView and its data are initialized. I've put this on my ListView but it doesn't work:

onCreationCompleted: {
    console.debug("[ListView.onCreationCompleted] selected: "+isSelected(0) );
    // The first item is selected by default.
    clearSelection();
    select(0,true);
}
onDataModelChanged: {
    console.debug("[ListView.onDataModelChanged] selected: "+isSelected(0) );
    // The first item is selected by default.
    clearSelection();
    select(0,true);
}

 

The ListView always starts with no selected item no matter what I do. The first time I trigger the item that supposed to be selected - the first item [0] - it is not selected yet.

 

Does anybody know a way to initialize a ListView with a selected item?

 

Thanks!

Please use plain text.
Developer
billbsb
Posts: 72
Registered: ‎03-25-2011
My Carrier: None

Re: Initialize ListView with selected item

Bump!

Nobody has any ideas? I'm still stuck.
Please use plain text.
Developer
strobejb
Posts: 181
Registered: ‎10-15-2012
My Carrier: Orange

Re: Initialize ListView with selected item

The documentation for ListView::select() is where you can find the answer

 

http://developer.blackberry.com/cascades/reference/bb__cascades__listview.html#function-select-index...

 

You need to pass an indexPath as the 1st parameter

Please use plain text.
Developer
dishooom
Posts: 103
Registered: ‎12-31-2012
My Carrier: Verizon

Re: Initialize ListView with selected item

[ Edited ]

 

 

To  get a ListView to position at a particular row,  you need to specify the indexPath of the required row as follows:

   lv.scrollToItem (_propMap.index, 0x0);
  lv.select(_propMap.index,true);

 scrollToItem() to scroll the listview to the selected index and select() to mark the row as selected.

 

_propMap.index is the indexPath of my rewuired element which i had determined from C++ side using model->find()

 

Alternatively you could hardcode the indexPath if u know it in advance. For instance,

lv.scrollToItem([0,4], 0x0);
 lv.select([0,4], true)

 

would work just as fine only if i knew in advance that the indexPath of the required row is [0,4]

 

-Dishooom

 

:Angel:

 

Please use plain text.