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
greenback
Posts: 515
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC
Accepted Solution

[Explanation needed] Clearing listview data, and repopulating with new data

Hello,

 

I have successfully populated a listview from c++, parsed data, and done some interesting things with it. However, I am stumbling drunk unable to figure out how to clear and repopulate my list view...



Question 1:

 

  1. After I insert data into the list view, how do I delete the data from the list and re-execute the same method (init) that populated the list view with data in the first place? 

These are the docs I've referenced and I do not understand how to implement the methods into my code.


                          QList - clear() method

                          https://developer.blackberry.com/cascades/reference/qlist.html#clear

 

                          Using Data Access Code Sample
                          
data_access/using_data_source

                          GroupDataModel - Clear()

                          https://developer.blackberry.com/cascades/reference/bb__cascades__groupdatamodel.html#clear



CODE

Here's my code - populating the list view from C++ and displaying the data in a ListView.

MyApp.cpp

MyApp::MyApp(bb::cascades::Application *app)
: QObject(app){
	// create scene document from main.qml asset
	    // set parent to created document to ensure it exists for the whole application lifetime
	    QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

	        AbstractPane *root = qml->createRootObject<AbstractPane>();
	        qml->setContextProperty("yoyo",this);
	        //grab references
	        list_view = root->findChild<ListView*>("listView");

	        // set created root object as a scene
	        app->setScene(root);

	        mNetworkAccessManager = new QNetworkAccessManager(this);
	        bool result = connect(mNetworkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(requestFinished(QNetworkReply*)));

	         Q_ASSERT(result);
	         Q_UNUSED(result);
json = new QFile("data/file.json"); } void MyApp::init(){ QNetworkRequest request = QNetworkRequest(); // i call some service here mNetworkAccessManager->get(request); } void MyApp::requestFinished(QNetworkReply *reply){ qDebug() << reply->error(); qDebug() << reply->errorString(); if (reply->error() == QNetworkReply::NoError) { qDebug() << "No error"; QByteArray data = reply->readAll(); if (!json->open(QIODevice::ReadWrite)) { qDebug() << "Failed to open file"; return; } json->write(data); bb::data&colon;:JsonDataAccess jda; QVariantMap results = jda.loadFromBuffer(data).toMap(); QVariantList lst = jda.loadFromBuffer(data).toList(); GroupDataModel *m = new GroupDataModel(); m->insertList(lst); m->setGrouping(ItemGrouping::None); if(list_view) list_view->setDataModel(m); }else{ showDialog("Boo",reply->errorString()); } }

 

MyApp.hpp

// Tabbed pane project template
#ifndef MyApp_HPP_
#define MyApp_HPP_

#include <QObject>
#include <QFile>
#include <QIODevice>
#include <bb/cascades/QmlDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>


namespace bb {
namespace cascades {
class Application;
}
}
namespace bb {
namespace data {
class Application;
}
}

/*!
 * @brief Application pane object
 *
 *Use this object to create and init app UI, to create context objects, to register the new meta types etc.
 */
class MyApp: public QObject {
Q_OBJECT
public:
	MyApp(bb::cascades::Application *app);
	virtual ~MyApp() {
	}
	Q_INVOKABLE
	void init();
private slots:
	void requestFinished(QNetworkReply *reply);
private:
	QNetworkAccessManager *mNetworkAccessManager;
	QNetworkRequest *request;
	QFile *json;
	bb::cascades::ListView *list_view;
};

#endif /* MyApp_HPP_ */

 

main.qml

import bb.cascades 1.0

Page{ 
Container {
                        background: Color.White
                        ListView {
                            id: listView
                            preferredHeight: maxHeight
                            objectName: "listView"
                            listItemComponents: [
                                ListItemComponent {
                                    type: "item"
                                    Container {
                                        Container {
                                            Label {
                                                text: ListItemData.id
                                            }
                                        }
 
                                    }
                                }
                            ]
                            onTriggered: {
                                console.log("selected_index: " + indexPath)
                            }
                            horizontalAlignment: HorizontalAlignment.Center
                            verticalAlignment: VerticalAlignment.Center
                        }

}
}

 
Question 2:

How do I reference the ListView object with name "listView" within different parts of MyApp.cpp file? Is there anything else that I should add to the header file to make the listView accessible?


Question 3:

What happens when you call the clear() method? How is the data model affected? How is the UI affected? What happens in memory?


Question 4:

How do you verify that the data in the List View has been cleared so that you can go ahead and repopulate it with new data? What's the check that I perform?


I would also like to know about how you all are managing mutliple list views and data sources in your applications. Any best practices or insights?


Thanks
:Rockon:



Please use plain text.
Developer
Dredvard
Posts: 160
Registered: ‎01-27-2012
My Device: Playbook
My Carrier: Rogers

Re: [Explanation needed] Clearing listview data, and repopulating with new data

Here's how I did it

                            DeleteActionItem {
                                title: qsTr("Delete Selected")
                                onTriggered: {
                                    var deleteList = rootplaylistitem.ListItem.view.selectionList();
                                    console.log(deleteList)
                                    rootplaylistitem.ListItem.view.dataModel.removeItems(deleteList);
                                }
                            }

 So essentially I accessed the dataModel and deleted the items from there.  I kept a displayed copy of the data, in addtion to any undisplayed data structure I wanted stored.    Acessing the dataModel,  you have control of the data and memory.

---
If you find my post helpful please "like" it and "accept as a solution"
Please use plain text.
Developer
Zmey
Posts: 1,511
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: [Explanation needed] Clearing listview data, and repopulating with new data

After I insert data into the list view, how do I delete the data from the list and re-execute the same method (init) that populated the list view with data in the first place? 

 

Don't create DataModel every time you repopulate ListView.

Create it as member variable of the class (declare it in header and create in constructor) and set it's parent to 'this' so it will be destroyed when the class is destroyed. Assign DataModel to ListView in constructor.

 

After fetching the data from network just clear() the datasource then add new data to it.

It will notify ListView to update it's visual items.

 

How do I reference the ListView object with name "listView" within different parts of MyApp.cpp file? Is there anything else that I should add to the header file to make the listView accessible?

 

There's already list_view member variable - it's visible in other methods too.

 

What happens when you call the clear() method? How is the data model affected? How is the UI affected? What happens in memory?

 

It depends on DataModel type. clear() will remove all items from model and notify ListView to update it's visual items. You can also update individual DataModel items so only these will be refreshed.

 

How do you verify that the data in the List View has been cleared so that you can go ahead and repopulate it with new data? What's the check that I perform?

 

No checks are needed. Just clear() & repopulate dataModel (these are synchronous calls).

 

I would also like to know about how you all are managing mutliple list views and data sources in your applications. Any best practices or insights?

 

I use ListItemProvider+DataModel combination. You can assign a single DataModel to multiple listviews if needed.

 

ListItemProvider creates & updates list items which display data from dataModel.

 

List item implementations can be reused on different screens.

 


Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
greenback
Posts: 515
Registered: ‎10-17-2010
My Device: BlackBerry Z10, DAC

Re: [Explanation needed] Clearing listview data, and repopulating with new data

I was able to clear the listview with the following code if anyone is interested.

 

listView.dataModel = null

 The dataModel must be set to "null"

Please use plain text.