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: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone
Accepted Solution

A simple example of populating data from Json file to a ListView.

[ Edited ]

I want a simple example of inflating a listview with Json data.

Sample examples are not working

 

like we have a sample code

 

app.cpp

 

App::App()
{
	Page *root = new Page;
	ListView *listView = new ListView;

	// Create the data model, specifying sorting keys of "firstName" and "lastName"
	GroupDataModel *model = new GroupDataModel(QStringList() << "firstName"
	                                           << "lastName");

	// Create a JsonDataAccess object and load the .json file. The QDir::currentPath()
	// function returns the current working directory for the app.
	JsonDataAccess jda;
	QVariant list = jda.load(QDir::currentPath() +
	                         "/app/native/assets/employees.json");

	// Insert the data into the data model. Because the root of the .json file is an
	// array, a QVariant(QVariantList) is returned from load(). You can provide a
	// QVariantList to a data model directly by using insertList().
	model->insertList(list.value<QVariantList>());


	qDebug()<<"the size of model is "<<model->size()<<"\n";

	// Set the data model for the list view
	listView->setDataModel(model);

	// Set the content of the page and display it
	root->setContent(listView);
Application::setScene(root); }

 

with employees.json file in assets

 

[
	{ 	
        "firstName" : "Mike",
		"lastName" : "Chepesky"
		"employeeNumber" : 01840192
	},
	{
		"firstName" : "Westlee",
		"lastName" : "Barichak"
		"employeeNumber" : 47901927
	},
	{ 	
        "firstName" : "Jamie",
		"lastName" : "Lambier"
		"employeeNumber" : 51239657
	},
	{ 	
        "firstName" : "Denise",
		"lastName" : "Marshall"
		"employeeNumber" : 41239520
	},
	{ 	
        "firstName" : "Matthew",
		"lastName" : "Taylor"
		"employeeNumber" : 01963597
	},
	{ 	
        "firstName" : "Mark",
		"lastName" : "Tiegs"
		"employeeNumber" : 65321951
	},
	{ 	
        "firstName" : "Karla",
		"lastName" : "Tetzel"
		"employeeNumber" : 03266987
	},
	{ 	
        "firstName" : "Ian",
		"lastName" : "Dundas"
		"employeeNumber" : 29472012
	},
	{ 	
        "firstName" : "Marco",
		"lastName" : "Cacciacarro"
		"employeeNumber" : 56446691
	}
]

 

On running  this shows a blank screen with no list control.

 



Always Success
Developer
Posts: 222
Registered: ‎11-05-2011
My Device: Z10
My Carrier: Beeline

Re: A simple example of populating data from Json file to a ListView.

you add file access in bar-descriptor.xml ?

Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: A simple example of populating data from Json file to a ListView.

Yes that permission is set in bar-descriptor.xml , bbdata library is also added in .pro file. Still no luck.

 

 

Always Success
Regular Contributor
Posts: 83
Registered: ‎07-19-2012
My Device: Bold 9790, Dev Alpha A
My Carrier: Koodo

Re: A simple example of populating data from Json file to a ListView.

The issue may be that you have a line disconnect in your code for the following

QVariant list = jda.load(QDir::currentPath() +
	                         "/app/native/assets/employees.json");

This needs to be all on a single line as opposed to 2 lines. I made the same mistake the first time I copied and pasted the code. Let me know if that solves the issue

Developer
Posts: 610
Registered: ‎10-17-2010
My Device: (BlackBerry Z10)-> Q10/Passport Dual Use

Re: A simple example of populating data from Json file to a ListView.

I found a simpler example that works right out of the box. It populates json from a file into a list view.

 

Just Create a New Cascades Project:

 

  1. File->New Cascades Project
  2. Give it a name
  3. Choose the List with context menu template

  4. Click finish

 

The IDE will populate a new project for you. I found this template very helpful.




Developer
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: A simple example of populating data from Json file to a ListView.

You can also check out the stamp collector example - which populates from a JSON file.  Here's the code from that example  In that example I notice they set the parent for the data model and you didn't.

    JsonDataAccess jda;
    
    QVariantList mainList = jda.load("app/native/assets/stamps.json").value<QVariantList>();

    if (jda.hasError()) {
        bb::data::DataAccessError error = jda.error();
        qDebug() << "JSON loading error: " << error.errorType() << ": " << error.errorMessage();
        return;
    }

    // A GroupDataModel is a helper class that the list uses for data handling.
    // We sort on region in the model, this way will get different categories.
    GroupDataModel *stampModel = new GroupDataModel(QStringList() << "region");
    stampModel->setParent(this);
    stampModel->insertList(mainList);
    stampModel->setGrouping(ItemGrouping::ByFullValue);

    stampList->setDataModel(stampModel);

 

---
If you find my post helpful please "like" it and "accept as a solution"
Developer
Posts: 80
Registered: ‎09-03-2012
My Device: 9810
My Carrier: Vodafone

Re: A simple example of populating data from Json file to a ListView.

Thanks all of you for your replies, actually problem was not in code. The problem was in employees.json file that I downloaded from developer's site .

 

[
	{ 	
        "firstName" : "Mike",
		"lastName" : "Chepesky"      // after this ',' is missing
		"employeeNumber" : 01840192
	},
	{
		"firstName" : "Westlee",
		"lastName" : "Barichak"      // after this ',' is missing
 		"employeeNumber" : 47901927
	},
	{ 	
        "firstName" : "Jamie",
		"lastName" : "Lambier"    // after this ',' is missing
		"employeeNumber" : 51239657
	},

....................so on
]

 

means error occurred while parsing json. Very silly mistake. I would have checked it earlier.

 

So whoever is using this sample json file in their apps. Please correct it first and then use.

 

Always Success
New Developer
Posts: 24
Registered: ‎12-08-2012
My Device: Z10 Ferrari Red
My Carrier: Rogers

Re: A simple example of populating data from Json file to a ListView.

Hey, can you tell me what you need to add to bar-descriptor.xml file to allow file access?

Thanks,
Daxal
Developer
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: A simple example of populating data from Json file to a ListView.

You shouldn't need anything to access a json file within that applications asset structure.  If you want to access a json  file on  internet or shard files that you would select those in the application page.

---
If you find my post helpful please "like" it and "accept as a solution"