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
Highlighted
Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none
Accepted Solution

Fill ArrayDataModel in QML and fill information in ListView

Hello,

 

from the filepicker I get the file + filepath of a file. Now I extract the path and the filename from the string "selectedFile".

 

Now I want to add those two strings to an ArrayDataModel, which shows the data in a listview, the filename as title and the filepath as description.

 

Here's my code:

 

// Main.qml

ListView { id: listview dataModel: ArrayDataModel { id: theDataModel } listItemComponents: [ ListItemComponent { StandardListItem { title: ListItemData <------------------ here I want to display the filename description: "filepath" <-------------- and here I want to display the filepath imageSource: "asset:///images/icon.png"

[...]


onFileSelected: {
selectedFile = selectedFiles[0];
[...]
    var filepath = *a string*
   var filename = *a string*
    theDataModel.append([filename, filepath]); <-------------- how can I add the two strings correctly to the datamodel?
}

 

 What my code does at the moment when I pick a file is that it adds two components to the list the first has the filename as title the second has the filepath as title.

 

How do I manage to give the filename and -path to the datamodel so that I can read both of them and put them into the list view?

// Main.qml

ListView {
  id: listview
                
  dataModel: ArrayDataModel {
    id: theDataModel
  }

  listItemComponents: [
    ListItemComponent {

      StandardListItem {

         title: ListItemData.name       <---------------------------------------------------|
         description: ListItemData.path  <-----------------------------------------------|
         imageSource: "asset:///images/icon.png"                                              |
                                                                                                                        |
[...]                                                                                                                  |
                                                                                                                        |
onFileSelected: {                                                                                             |
  selectedFile = selectedFiles[0];                                                                     |
  [...]                                                                                                                |
    var filepath = *a string*                                                                              |
    var filename = *a string*                                                                            |
    theDataModel.append([filename, filepath]); <----------- how do I add the strings correctly so that I can us them in the list
}

 

Developer
Posts: 108
Registered: ‎04-14-2013
My Device: BlackBerry Z30
My Carrier: Chunghwa Telecom

Re: Fill ArrayDataModel in QML and fill information in ListView

Hi!

 

For the arrayDataModel, I suggest you use this

 

var data = {
"name": filename,
"path": filepath
}

theDataModel.append(data);

 

If you still have any problem, please let me know!

 

Simon

 

BlackBerry Certified Cascades Developer
Developer
Posts: 102
Registered: ‎04-01-2014
My Device: BlackBerry10 Simulator
My Carrier: none

Re: Fill ArrayDataModel in QML and fill information in ListView

Thanks a lot. That works great.