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
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10
Accepted Solution

GroupDataModel doesn't sort when data is added from Json

[ Edited ]

If I have an empty Json and I add data to it one by one, everything works just fine but when I have a data in Json it doesnt sort. Let's say following is my json:

 

[
   {
      "DateCreated" : 1383137332,
      "DateToFinish" : "1383137400",
      "Description" : "Task 3",
      "Status" : "1",
      "taskId" : "3"
   },
   {
      "DateCreated" : 1383137327,
      "DateToFinish" : "1383137400",
      "Description" : "Task 2",
      "Status" : "1",
      "taskId" : "2"
   },
   {
      "DateCreated" : 1383137321,
      "DateToFinish" : "1383137400",
      "Description" : "Task 1",
      "Status" : "1",
      "taskId" : "1"
   }
]

 sortingKey is only set to "taskId" and sortedAscending is set to false with "setSortedAscending(false)" but in ListView, it shows in Ascending order like "Task1, Task2, Task3". I fill the model from json with:

 

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

	this->insertList(list.value<QVariantList>());

 Additionally, after I load and insert json to Model, when I try to add a new element to model, it doesn't add correctly in its place. It would add "task4" with id "taskId: 2" at the top of json file...

 

EDIT: `jda` is `bb::data&colon;:JsonDataAccess`

Contributor
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10

Re: GroupDataModel doesn't sort when data is added from Json

[ Edited ]

Putting "setSortedAscending(false)" after inserting solved it but I will leave this open for explanation.

Retired
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30

Re: GroupDataModel doesn't sort when data is added from Json

That sounds like it could be a bug. Can you please log it on the Developer Issue Tracker? DIT

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Contributor
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10

Re: GroupDataModel doesn't sort when data is added from Json

Sure. I will test it tomorrow, just to be 100% sure and then report.
Retired
PBernhardt
Posts: 749
Registered: ‎12-16-2008
My Device: BlackBerry Z30

Re: GroupDataModel doesn't sort when data is added from Json

Thank you!

Paul Bernhardt
Application Development Consultant
BlackBerry
@PBernhardt

Did this answer your question? Please accept this post as the solution.
Found a bug? Report it to the Developer Issue Tracker
Contributor
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10

Re: GroupDataModel doesn't sort when data is added from Json

I am sorry but I am unable to access Developer Issue Tracker website. When I log in, it keeps refreshing but nothing happens.
Developer
Bluenoser
Posts: 313
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: GroupDataModel doesn't sort when data is added from Json

How do you add the new element to the model? Can you post the code?

Contributor
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10

Re: GroupDataModel doesn't sort when data is added from Json

I have posted how I load from Json.


 Also, here is how I add elements:

 

QVariantMap newTask;
	//newTask["taskId"] = QString::number(lastId+1);
	newTask["Description"] = description;
	newTask["Status"] = QString::number(1);
	newTask["DateToFinish"] = QString::number(dateToFinish.toTime_t());
	newTask["DateCreated"] = QDateTime::currentDateTime().toTime_t();
	//Insert before writing to Json
	//To be able to write Json file
	this->insert(newTask);

	//Gluing all tasks together
	QVariantList entireDataList;
	for (QVariantList it = this->first(); it != this->last(); it = this->after(it)) {
		entireDataList << this->data(it);
	}
	entireDataList << this->data(this->last());
	QVariant entireData = (QVariant)entireDataList;

	//Save Json file
	jda->save(entireData, QDir::currentPath() + "/app/native/assets/test.json");
	if (!jda->hasError()) {
		emit newTaskAdded();
	}else {
		//Remove the problematic task from model if there is a problem in writing to json
		this->remove(newTask);
	}

 

Also,

for (QVariantList it = this->first(); it != this->last(); it = this->after(it)) {
		entireDataList << this->data(it);
	}

 this is because when I wanted to save to json by filling "entireDataList" without using the "model", it was saving weirdly.

Developer
Bluenoser
Posts: 313
Registered: ‎04-04-2012
My Device: BlackBerry Z30/Z10/Z10LE/Q10/PlayBook

Re: GroupDataModel doesn't sort when data is added from Json

Thanks for posting. I was wondering as I had a similar problem. I loaded the JSON file and then used it to replicate data from itself. It wasn't sorting properly. It was because of the key elements not being strings.

 

http://supportforums.blackberry.com/t5/Native-Development/GroupDataModel-Updating-Data/m-p/2652951#M...

Contributor
khajvah
Posts: 15
Registered: ‎10-24-2013
My Device: z10

Re: GroupDataModel doesn't sort when data is added from Json

Thanks, I will look at it as soon as I have free time :smileyhappy: