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
Developer
Posts: 676
Registered: ‎05-06-2012
My Device: Dev Alpha & PlayBook
My Carrier: wifi
Accepted Solution

Get first item in data model

I'm trying to get the first item in a data model. I would expect the following to work but it returns -1

 

var firstItem = myDataModel.indexOf(0)

 

_________________________________________________________
co-founder of Diaree | BB Dev for PictureThis
Highlighted
Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: Get first item in data model

[ Edited ]

IndexPath is an array, and data() should return an element at specified indexPath:

if (myDataModel.size() > 0) {
  var firstItem = myDataModel.data([0])
}

 

indexOf searches for a value and returns it's indexPath.

  

UPD:

 

In ArrayDataModel there's also .value() function which takes a plain index.

data() is more generic function inherited from DataModel.

 

So, it can be simplified to:

 

var firstItem = myDataModel.value(0)

 


Andrey Fidrya, @zmeyc on twitter