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
Contributor
Posts: 10
Registered: ‎05-20-2013
My Device: Z 10
My Carrier: Blackberry

3 ListView with 3 DataModels

[ Edited ]

Hi friends,

  In my app Iam having a page with 3 buttons on the top and When I click any of the button I want corresponding data from DB displayed on ListView.

 

What I have tried:

   To solve this I tried to use 3 ListViews(display only one at a time and disabling others)

   Each ListView I have groupDataModel

   And I inserted data to them from DB using SQL queries 

 

Below is the code that I tried to create a ListView and load data from DB to it

 ListView {
                id: appList
                dataModel: articlesDataModel
                listItemComponents: [
                    ListItemComponent {
                        type: "item"
                            Container {

                                layout: StackLayout {}
                         
                                   Label {
                                    id: titleLabel
                                    text: ListItemData.title
                                }
                                Label {
                                    id: descLabel
                                    text: ListItemData.shortDesc
                                }

 
                        }                     

    } //outerContainer
    attachedObjects: [

        GroupDataModel {
            id: articlesDataModel
            grouping: ItemGrouping.None

        },

        // Custom data source object, loads data using a SqlConnection, see customsqldatasource.cpp (this class is used in Quotes example)
        CustomSqlDataSource {
            id: asynkDataSource

            source: "db/duDatabase.db"
            query: "SELECT title,shortDesc,imagePath,description FROM how_to ORDER BY howToId DESC"
            property int loadCounter: 0

            onDataLoaded: {
                if (data.length > 0) {
                    articlesDataModel.insertList(data);

                }
            }
        }
    ]
    onCreationCompleted: {
        asynkDataSource.load();
    }
}

 

My problem is:

 

 Like the code above I tried to add 2 more ListView and corresponding GroupDataModel and insert data to model by changing the query.But this is not working.

 

Is this approach correct.Can I place 3 ListView and give them separate dataModels and Load them.Plz PLz help me out.Iam stuked here.

 

 

 

 

   

 

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: 3 ListView with 3 DataModels

First of all: why do you use three ListViews? Just use one and swap the data model.

 

Secondly: what do you mean by not working? This is rather vague: did your app crash, did you device explode, space radiotion interference...?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10
My Carrier: Blackberry

Re: 3 ListView with 3 DataModels

[ Edited ]

Can u plese give an idea about how to swap the DataModel if iam using the above code to create a ListView and DataModel

Contributor
Posts: 10
Registered: ‎05-20-2013
My Device: Z 10
My Carrier: Blackberry

Re: 3 ListView with 3 DataModels

Hi Zezke,
Thanks a lot for your reply.Actually while I tried my code as I described above my app crash.
Now Iam trying your opinion.Could you please tell me how to swap the DataModel.Is it is possible using the above code that I have include??
Plz give an idea about how to swap the DataModel
Contributor
Posts: 10
Registered: ‎05-20-2013
My Device: Z 10
My Carrier: Blackberry

Re: 3 ListView with 3 DataModels

[ Edited ]

Plz help me out zezki,

    How can we swap DataModels.

 

   I was trying to add 2 more groupDataModel and corresponding DataSource with approapriate query.And On the button click (where I want to change the content of the List Item) I tried to change the listView DataModel.

 

The Problem is:

OnCreationComplete od the page while Iam trying to load all the three DataSources Then the app crashes,

So Iam Loading the DataSouce on the Button Click.At first time when the button is clicked then the list loads with correct data,But when I again try to ckick any buttons the the app crashes.Is this the way to swap

 

 

Any one Plz Help me

Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: 3 ListView with 3 DataModels

Do you want to swap the data model in C++ or QML?

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Contributor
Posts: 10
Registered: ‎05-20-2013
My Device: Z 10
My Carrier: Blackberry

Re: 3 ListView with 3 DataModels

[ Edited ]

Iam swapping in QML.Iam attaching the code That I have tried so far.

import bb.cascades 1.0
import bb.data 1.0
import com.du.data 1.0

Page {
    Container {
        TopBar {
            title: "How to Du"
        }
        Container {
            Button{
                id: how_To
                text:"how_To"
                imageSource: "asset:///images/1371654231_twitter.png"
                onClicked: 
                    {
                        how_To.enabled = false;
                        tutorial.enabled = true;
                       appsFromDu.enabled = true;
                      console.log("how_To button");
                        asynkDataSource.load();
                        appList.dataModel = articlesDataModel;
                    }

            }
            Button {
                id: tutorial
                text: "tutorial"
                imageSource: "asset:///images/1371654231_twitter.png"
                onClicked: {
                    how_To.enabled = true;
                    tutorial.enabled = false;
                    appsFromDu.enabled = true;
                    console.log(" tutorial button");
                    asynkDataSource1.load();
                    appList.dataModel = articlesDataModel1;
                }

            }
            Button {
                id: appsFromDu
                text: "AppsFromDu"
                imageSource: "asset:///images/1371654231_twitter.png"
                onClicked: {
                    how_To.enabled = true;
                    tutorial.enabled = true;
                    appsFromDu.enabled = false;
                    console.log(" AppsFromDu button");
                    asynkDataSource2.load();
                    appList.dataModel = articlesDataModel2;
                }

            }
            ListView {
                id: appList
                dataModel: articlesDataModel
                listItemComponents: [
                    ListItemComponent {
                        type: "item"
                            Container {

                                layout: StackLayout {
                                }
                                Label {
                                    id: titleLabel
                                    text: ListItemData.title
                                }
                                Label {
                                    id: descLabel
                                    text: ListItemData.shortDesc
                                  
                                }
                            }    
    } 
    attachedObjects: [

        GroupDataModel {
            id: articlesDataModel
            grouping: ItemGrouping.None

        },

        // Custom data source object, loads data using a SqlConnection, see customsqldatasource.cpp
        CustomSqlDataSource {
            id: asynkDataSource

            source: "db/duDatabase.db"
            query: "SELECT title,shortDesc,imagePath,description FROM how_to ORDER BY howToId DESC"
            //property int loadCounter: 0

            onDataLoaded: {
                console.log("loading dataModel");
                if (data.length > 0) 
                {
                    articlesDataModel.clear();
                    articlesDataModel.insertList(data);

                }
            }
        },

        GroupDataModel {
           id: articlesDataModel1
            grouping: ItemGrouping.None

        },


        CustomSqlDataSource {
            id: asynkDataSource1
            source: "db/duDatabase.db"
            query: "SELECT title,shortDesc,imagePath,description,externalLink FROM tutorial ORDER BY tutorialId DESC"
           // property int loadCounter: 0
            onDataLoaded:
                {
                    console.log("loading dataModel1");
               if (data.length > 0) 
               {
                    articlesDataModel1.clear();
                    articlesDataModel1.insertList(data);

               }
            }
        },
        GroupDataModel {
            id: articlesDataModel2
            grouping: ItemGrouping.None

        },


        CustomSqlDataSource {
            id: asynkDataSource2
            source: "db/duDatabase.db"
            query: "SELECT title,shortDesc,imagePath,description FROM app_detail ORDER BY appId DESC"
           // property int loadCounter: 0
            onDataLoaded: {
                console.log("loading dataModel2");
                if (data.length > 0) {
                    articlesDataModel2.clear();
                    articlesDataModel2.insertList(data);

                }
            }
        }

    ]
    onCreationCompleted: 
        {
            how_To.enabled = false;
            console.log("loaded dataSources");
            asynkDataSource.load();
       
    }
}

Here at first when I click on the 3 button all works fine.After that when I again click on any that I have clicked once then the app crash.Plz Plz help me out zezke

Developer
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10
My Carrier: Blackberry

Re: 3 ListView with 3 DataModels

[ Edited ]

Any one plz help.Iam also stucked with this issue Smiley Sad

Highlighted
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: 3 ListView with 3 DataModels

I created a little sample to show you guys how to swap the model. I used a XmlDataModel, but the principle should be the same.

 

import bb.cascades 1.0

Page {
    Container {
        layout: StackLayout {
            orientation: LayoutOrientation.TopToBottom
        }
        topPadding: 20
        SegmentedControl {
            id: dataModelSelection
            Option {
            	text: "Beer"
            	value: 1
            }
            Option {
                text: "Pizza"
                value: 2
            }
            Option {
                text: "BlackBerry"
                value: 3
            }
            onSelectedIndexChanged: {
                var dataModel
                switch (dataModelSelection.selectedValue)
                {
                    case 1:
                        dataModel = firstModel
                        break
                    case 2:
                        dataModel = secondModel
                        break
                    case 3:
                        dataModel = thirdModel
                        break
                        
                }
                listView.dataModel = dataModel
            }
        }
        ListView {
            id: listView
            dataModel: firstModel
        }
    }
    attachedObjects: [
        XmlDataModel {
            id: firstModel
            source: "asset:///data/1.xml"
        },
        XmlDataModel {
            id: secondModel
            source: "asset:///data/2.xml"
        },
        XmlDataModel {
            id: thirdModel
            source: "asset:///data/3.xml"
        }
    ]
}

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!