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: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus
Accepted Solution

Finding string in arraydatamodel

[ Edited ]

Sorry if this has been asked before but I'm having a hard time finding a sample that shows how to search for a string inside a datamodel.  

 

I have an ArrayDataModel which populates a listview and works great.  What I want to do is access the datamodel from outside the listview and pull data from it.  I have an imagebutton that when pushed I want to search the datamodel for a variable and tell me what line number in the array that string is.  

 

I've been trying to ise .indexOf and a For loop but I can't seem to get it to return anything but -1 even though I know for a fact the test string is in every line. 

 

Again, sorry if this has already been asked.  New to this and I'm stumped. I suspect it's pretty basic for anyone with experience. 

Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Finding string in arraydatamodel

And your items in model is JSON objects or just a string?

Code you are using for search will help us to help you



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Finding string in arraydatamodel

Honestly, I'm not sure if it's JSON.  I grab the data from an XML feed and put it into a an ArrayDataModel. 

 

Here is the ListView

            ListView {

                id: scheduleDataModelListview

                dataModel: scheduleDataModel

                function itemType(data, indexPath) {

                    return (indexPath.length == 1 ? 'item' : 'header')

                }

 

 

 

Here is the attached Datamodel/Datasource:

           attachedObjects: [

                ArrayDataModel {

                    id: scheduleDataModel

 

                },

                DataSource {

                    id: scheduleDataSource

                    source: "http://---------------------------------"

                    query: "/data/row"

                    type: DataSourceType.Xml

                    onDataLoaded: {

                        scheduleDataModel.clear();

                        scheduleDataModel.append(data);

 

                    }

                    remote: true

                } //end data source

            ]

 

            onCreationCompleted: { //scorboardIndicator.stop();

                scheduleDataSource.load();

 

            }

 

 

I don't really have anything to show for what I've tried since none of it has worked.  But, it was along these lines:

 

var scoreboardIndexOf = new Array()

scoreboardIndexOf[0] = 'Halifax'

console.log('indexOf: ' + scheduleDataModel.size() + scoreboardIndexOf)

 

var test = new Array()

test = scheduleDataModel

//for (var i = 0; i < scheduleDataModel.size(); i ++)console.log('test: ' + test.indexOf(scoreboardIndexOf));

Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Finding string in arraydatamodel

[ Edited ]

So your items are JSON objects. This means that you can't use indexOf for searching just a string you need to search whole item object

 

1. for example if i have ArrayDataModel with this type of items {firstname: "test", lastname: "test"} then i can't use indexOf like this

model.indexOf("test")

 but like this

model.indexOf({"firstname": "test", "lastname": "test"})

 2. I can use for loop to search item with matching one property like this

for example i want to search item by firstname

var indexPath;
for (var i = 0; i < model.size(); ++i){
    var item = model.data([i])
    if (item.firstname == "test"){
        indexPath = [i]
        break;
    }
}
console.log("Index of found item: "+[i]);

 Hope it helps Smiley Wink



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Finding string in arraydatamodel

Awesome, thanks! I will try that out when I am at  my computer later.  Is there a way to display what the JSON format looks like in the console.log?  

 

The part I'm trying to search on is the rowId from this (abbreviated) XML. 

 
<data serverTime="2014-08-02 08:38:29.679" offset="-240">
<script id="tinyhippos-injected"/>
<row recordDate="2014-11-23 16:00:00.000" updateDate="2014-07-08 18:34:53.000" rowId="1678923">
<value key="template">TableRow_ScheduleImage</value>
<value key="Image_AwayImage">
<url>
--------------------------------------------
</url>
</value>
<value key="Image_HomeImage">
<url>
GetImage.aspx?type=thumb&format=jpg&id=159&lang=english&w=48&h=48
</url>
</value>
...
</row>
</data>
Developer
Posts: 543
Registered: ‎08-31-2013
My Device: Q10, Z10LE, Nokia N950, DAB....
My Carrier: Telekom Slovakia

Re: Finding string in arraydatamodel

Yes you can use JSON.stringlify() function

onDataLoaded:{
    console.log(JSON.stringlify(data[0))
}

 



If helped give a like. If your problem was solved mark the post as "Solution"
Portfolio: BlackBerry Nokia
Social: Twitter | Facebook
About Me / Blog DevPDA | Blog
New Developer
Posts: 22
Registered: ‎01-20-2014
My Device: z10
My Carrier: vietel

Re: Finding string in arraydatamodel

Luckily, I got the same problem. Now I can fixed this. Thank you.

New Developer
Posts: 41
Registered: ‎08-02-2014
My Device: BlackBerry Z10
My Carrier: Wind Mobile

Re: Finding string in arraydatamodel

[ Edited ]

Isn't it JSON.stringify()

No "l"

onDataLoaded:{
    console.log(JSON.stringify(data[0))
}
Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Finding string in arraydatamodel

I've only had time to test this part but yeah, a couple small typos.   It works though, so thank you very much.  Will test the main piece this evening. 

 

This is what I used to find the JSON data&colon;

console.log('JSON log' + JSON.stringify(ListItemData));

 

I put it in the onCreationComplete of the Listview, ListItemComponent. 

Highlighted
Developer
Posts: 26
Registered: ‎07-22-2014
My Device: Z30, Z10, Q10, Passport
My Carrier: Telus

Re: Finding string in arraydatamodel

FInally had time to test it, and it worked like a charm. Thanks very much for your help!  Here is the final version of my code:

 

 

This is part of the onClicked of an imageButton

 

                                        var indexPathCal;

                                        for (var i = 0; i < Qt.scheduleDataModel.size(); ++i){  

//checks the size of the arrayDataModel I want to find the string in

                                            var item = Qt.scheduleDataModel.data([i])

                                            if (item.rowId == ListItemData.value[6].text){

//finds the value from the current listview's arrayDataModel (where the button is) to the value

//of rowId in the other arrayDataModel

                                                indexPathCal = [i]

                                                break;

                                            }

                                        }

                                        console.log("Index of found item: "+[i]);

 

//This line scrolls the other listview so the row that matches ListItemData.value[6].text is in view                                         

                                        Qt.scheduleDataModelListview.scrollToItem(indexPathCal, ScrollAnimation.Default)