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
tanphat1412
Posts: 123
Registered: ‎11-06-2012
My Device: BlackBerry
My Carrier: Mobifone

How to get indexPath from ListView

I'm using GoupDataModel to show data in ListView.

Now  I need to find record(s) in the list, can any body help me to loop throgh the datamodel to get the indexPath? I've tried to use datamodel.find(), but it doesn't work.

 

Thanks in advanced.

Please use plain text.
Developer
Pumano-
Posts: 215
Registered: ‎11-05-2011
My Device: Z10
My Carrier: Beeline

Re: How to get indexPath from ListView

in QML list:

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

 

Please use plain text.
Developer
tanphat1412
Posts: 123
Registered: ‎11-06-2012
My Device: BlackBerry
My Carrier: Mobifone

Re: How to get indexPath from ListView

Sorry for my bad english, I means to search an indexPath of a record in listview

Please use plain text.
Developer
AlexXF
Posts: 323
Registered: ‎03-10-2010
My Device: Bold 9780, DevAlpha
My Carrier: Vodafone CZ

Re: How to get indexPath from ListView

indexPath is a simple JS array, where first element have a index.

 

var indexPath = new Array();
indexPath[0] = 0; // get first element of DataModel
var item = dataModel.data(indexPath);

 

Please use plain text.
Developer
tanphat1412
Posts: 123
Registered: ‎11-06-2012
My Device: BlackBerry
My Carrier: Mobifone

Re: How to get indexPath from ListView

So how I can search an indexpath which content is alike a keyword in groupdatamodel.

Example: I have a listview contains all of the name of country in the world. How do i get the indexPath of Singapore and make the listview scroll to Singapore.

Please use plain text.
Developer
Zmey
Posts: 1,507
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: How to get indexPath from ListView

DataModel has childCount method so just iterate elements from 0 to childCount - 1.

You can create indexPath for each element then request data using .data method.

 

Once you know indexPath of required element, use ListView's scrollToItem method.

 

 

 


 

Another approach is to filter the data based on search string then repopulate the DataModel, so only relevant entries are shown.

 

You can also create another DataModel which will expose only a subset of original DataModel:

 

http://supportforums.blackberry.com/t5/Cascades-Development-Knowledge/Using-a-Custom-DataModel-to-Fi...


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
minmax
Posts: 46
Registered: ‎09-25-2012
My Device: Dev Alpha, Z10
My Carrier: MTS

Re: How to get indexPath from ListView

[ Edited ]
Please use plain text.
Developer
superdirt
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: How to get indexPath from ListView

[ Edited ]

Two ways:

1) Add a function to your DataModel class that returns an index path of a data item. Since the internals of DataModel are fully customizable, you can create any logic you want for defining index paths, just as long as the function uses logic that complements the data() function.

 

2) Expanding on Zmey's point, iterate through the DataModel children recursively until you find the object you are looking for. Keep track of the path while you search.

 

Get the DataModel from your ListView using dataModel(). Get the data qvariant from your ListItem using ListItem.data(). In the DataModel there are the childCount() and data() methods that you can use to iterate through all the objects in your data model. Check to see if the data() returns the same value of your ListItem.data(). If it does, you know you have the right indexPath - if not you have to try another indexPath.

 

There might be better, more direct ways but these should work. Option #1 is the most efficient but option #2 might be necessary if you can't change your DataModel definition.

 

Scott

Please use plain text.
Developer
minmax
Posts: 46
Registered: ‎09-25-2012
My Device: Dev Alpha, Z10
My Carrier: MTS

Re: How to get indexPath from ListView

Thank you for reply! :smileyhappy:
I think 1 way more prefered for me.
Please use plain text.
Developer
superdirt
Posts: 541
Registered: ‎05-17-2009
My Device: Not Specified

Re: How to get indexPath from ListView

You're welcome. That's good. Option 1 is how DataModels are supposed to be used :smileyhappy:
Please use plain text.