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
SumiSadiq
Posts: 148
Registered: ‎04-08-2013
My Device: Blackberry 10

Swapping dataModels of ListView

Hi friends,

   Iam having a page containing 3 buttons and a listView

 

scenerio

 When I click on a button corresponding data should be loaded from DB and listed in the ListView.

 

what I have tried:

 

To implement this I used 3 dataModels,When one button is pressed corresponding dataModel is loaded and displayed in ListView.Below is the code that I have tried

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;
                    
                        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 {
                                Label {
                                    id: titleLabel
                                    text: ListItemData.title
                                }
                                Label {
                                    id: descLabel
                                    text: ListItemData.shortDesc
                                }
                        } //container
                ]
            }
        } 
    } 
    attachedObjects: [
        GroupDataModel {
            id: articlesDataModel
            grouping: ItemGrouping.None

        },

        // Custom data source object, loads data using a SqlConnection
        CustomSqlDataSource {
            id: asynkDataSource

           source: "db/duDatabase.db"
            query: "SELECT title,shortDesc,imagePath,description FROM how_to ORDER BY howToId DESC"
          
            onDataLoaded: {
                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"
        
            onDataLoaded:
                {
                  
               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"
         
            onDataLoaded: {
               
                if (data.length > 0) {
                    //articlesDataModel2.clear();
                    articlesDataModel2.insertList(data);

                }
            }
        },
        ComponentDefinition {
            id: howtoDetailPage
            source: "HowtoDuDetails_EN.qml"
        }

    ]
    onCreationCompleted: 
        {
            how_To.enabled = false;
         
            asynkDataSource.load();
       
    }
}

 problem is:

 

When I clicked the 3 buttons for the first time then all works fine.Now when I tried to press any of the button again then the app crashes.

 

My question is:

 

Is this the way to swap DataModels.Can anyOne fine any error on my code why is it getting crashed.PLz PLz help me out.Iam stucked here :smileysad:

Please use plain text.
Developer
zezke
Posts: 1,004
Registered: ‎12-12-2010
My Device: Z10 (red Limited Edition)

Re: Swapping dataModels of ListView

What does the device log show? Did you try debugging?

-------------------------------------------
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!
Please use plain text.