xmllistviewblackberry-10blackberry-cascadesdatamodel

How to load source data in GroupDataModel for ListView in Blackberry 10 cascade?


Here is my line of code where I want to load source: data from xml file which is located in my device shared folder.

The path of that xml file is QFile textfile("/accounts/1000/shared/documents/myData.xml");

My code is:

import bb.cascades 1.0
import bb.data 1.0
Page {
content: ListView {
id: listView
dataModel: dataModel
    ...
}
attachedObjects: [
    GroupDataModel {
    id: dataModel
    },
        DataSource {
      id: dataSource


     //---------------------------------------
     //Here I want to load xml file
     //---------------------------------------
      source: "/accounts/1000/shared/documents/myData.xml"
    //---------------------------------------


      query: "/contacts/contact"
      onDataLoaded: {
        dataModel.insertList(data);
      }
    }
  ]
  onCreationCompleted: { dataSource.load(); }
}

Anyone please help me, how exactly load xml file in GroupDataModel which is located in above device directory location.

Thanks in advance.


Solution

  • we have two parts to do this :

    First one allowing your application to use shared folders

    Steps:

    1. Go to bar-descriptor.xml in your project
    2. select Application
    3. check true in shared files

    Second one is Get the correct path using C++ and send it to Qml in context property

    This in your ApllicationUI.cpp C++ Code

    // Build the path, add it as a context property, and expose
    // it to QML
    QString workingDir = QDir::currentPath();
    QString path = "file://" + workingDir +"/shared/documents/model.xml";
    QDeclarativePropertyMap* dirPaths = new QDeclarativePropertyMap;
    dirPaths->insert("documents", QVariant(QString(path)));
    qml->setContextProperty("dirPaths", dirPaths);
    

    QML

        dataModel: XmlDataModel {
                source: dirPaths.documents
        }