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.

inside custom component

Native Development

Reply
Highlighted
Developer
Posts: 169
Registered: ‎03-25-2011
My Device: Playbook, Z10 L.E.
My Carrier: Vodafone NL

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!

Cheers!
Developer
Posts: 169
Registered: ‎03-25-2011
My Device: Playbook, Z10 L.E.
My Carrier: Vodafone NL

Re: Initialize ListView with selected item

Bump!

Nobody has any ideas? I'm still stuck.
Cheers!
Developer
Posts: 293
Registered: ‎10-15-2012
My Device: bb10 developer
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

Developer
Posts: 103
Registered: ‎12-31-2012
My Device: Blackberry 9810
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

 

 

Contributor
Posts: 12
Registered: ‎06-25-2013
My Device: Z 10
My Carrier: Verizon

Re: Initialize ListView with selected item

I am having trouble with the same thing.  It seem that the selections are cleared after the onCreationCompleted event. I have tried using the main container onCreationCompleted event and the TabbedPane I am using onCreationCompleted event to set the initial selected item in the listview but it does not get selected. 

 

There must be some way to initialize a list view with a selected item.

There is a slected property of the container in the listitem.  I have tried using

that but I get the same results.

Developer
Posts: 88
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: Initialize ListView with selected item

You can try select([0],true) than select(0,true);
Contributor
Posts: 12
Registered: ‎06-25-2013
My Device: Z 10
My Carrier: Verizon

Re: Initialize ListView with selected item

I tried that but my program hangs.

 

onCreationCompleted: {
     if(ListItemData.channelName == "home") { 
          select([0],true);
         //select(0,true);
      }
}

Contributor
Posts: 12
Registered: ‎06-25-2013
My Device: Z 10
My Carrier: Verizon

Re: Initialize ListView with selected item

Actually it does not hang but nothing gets selected.

 

onCreationCompleted: {
     if(ListItemData.channelName == "home") { 
          select([0],true);
         //select(0,true);
      }
}

Developer
Posts: 88
Registered: ‎01-05-2013
My Device: BB10
My Carrier: Viettel

Re: Initialize ListView with selected item

You cant not detect your ListItemData like that. You must me indicate the indexPath of listitem to access to it's data.

for( ;Smiley Wink // here is your command to traversal the listview to get item
if(mListView.dataModel.data(yourIndexPath).channelName == "home"{
select(yourIndexPath,true);
}
Contributor
Posts: 12
Registered: ‎06-25-2013
My Device: Z 10
My Carrier: Verizon

Re: Initialize ListView with selected item

Even when I get the rigth data for the logic the item still does not get selected.