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
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

sharing data from a list item

Hi AllSmiley Happy

 

Im slightly convinced that what I'm trying to do is impossible(but that cant be true, right?). Anyway. I have a list item that pops up at the start of my app and I want to be able to share the data within by triggering a share Action Item, **NOT by selecting the list item**.

Problem is the data comes from another QML(listitem.qml) not the main QML.

so how would I pull the data that off of the listitem QML and invoke share on main?

 

Thanks,

   -Ali

 

here is my main qml action item code:

 

InvokeActionItem {
                                id: shareaction
                                title: qsTr("Share")
                                imageSource: "images/share.png"
                                ActionBar.placement: ActionBarPlacement.OnBar
                                query {
                                                mimeType: "text/plain"
                                                invokeActionId: "bb.action.SHARE"
                                            }
                                onTriggered: {
                                    data: {ListItemData.Name + ": " + ListItemData.Position}
                                    
                                }
                            }

 the above code opens share but has zero info attached :/

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: sharing data from a list item

[ Edited ]

You can access the ListView and its model from the listitem either of which could hold your code to do the share...

 

itemName.ListItem.view.dataModel.shareThis(ListItemData.name);

or

itemName.ListItem.view.shareThis(ListItemData.name);

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
Developer
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

Re: sharing data from a list item

thanks for the reply, will try it now Smiley Happy

Developer
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

Re: sharing data from a list item

Not sure if your suggestion works or not, Im almost positive I am just not doing it right. here is what I got(simplified):

 

page with the list(basket.qml)

Container {
        ListView {
            id: fruitList
            dataModel: XmlDataModel {
                source: "models/fruitmodel.xml"
            }
            listItemComponents: [
                ListItemComponent {
                    type: "fruit"
                    FruitListItem {
                        id: fruitItem
                        visible: {
                            if (ListItemData.ColorType == 1) {
                                return true;
                            } else {
                                return false;
                            }
                        }
                    }
                }
            ]
        }
    }

 

then the display model(FruitListItem.qml):

Container {
        label {
            text: ListItemData.Fruit
        }
    }

 

then on the main.qml, within the 'share' action item I have:

InvokeActionItem {
                                id: shareaction
                                title: qsTr("Share")
                                imageSource: "images/share.png"
                                ActionBar.placement: ActionBarPlacement.OnBar
                                query {
                                                mimeType: "text/plain"
                                                invokeActionId: "bb.action.SHARE"
                                                
                                            }
                                onTriggered: {
                                    fruitList.ListItem.view.dataModel.shareThis(ListItemData.Fruit);

                                     // or

                                     // fruitList.ListItem.view.shareThis(ListItemData.Fruit);
                                }
}

 

?

still pulls up a blank text field, but like I said, Im probably doing something wrong

 

-Ali

 

 

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: sharing data from a list item

ListItemData only exists in top level item of ListItemComponent. In the code above that item is FruitListItem {}.

 

For InvokeActionItem in main.qml you'll have to fetch the data from dataModel directly, by using an indexPath of selected item.


To obtain the currently selected indexPath from listView this function can be used:
http://developer.blackberry.com/cascades/reference/bb__cascades__listview.html#function-selected

 

The final code should look like this:

 

onTriggered: {
var selectedIndexPath = fruitList.selected()
var selectedItem = dataModel.data(selectedIndexPath)
console.log('Fruit: ' + selectedItem.Fruit);
data = selectedItem.Fruit; // not sure about this line
}

Adjust the id-s, additional qualifiers might be needed as you have complex qml structure.

 

I suggest checking the logs to ensure that there is no "variable not found" type of errors and that onTriggered was called and the fruit was successfully fetched from the model. 

 

Also, I'm not exactly sure, but you might have to convert the text to QByteArray before assigning it to InvokeActionItem's data. If this is the case, this can be done by using a C++ helper function.

 

If this won't work I'd create a single-page test project to isolate the problem.

 

Also I recall that there were problems with Invocation APIs in QML so there's a chance that the above will not work at all. Launching invocation from C++ code is very simple though. If you decide to try this approach I can try to find a  code snippet.

 


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

Re: sharing data from a list item

Thank you for your assistance. unfortunately my list has no selection abilities by design. I wrote the code to show only one item from that list... so its more of me trying to share the list item data that is visible rather than selected. 

 

thanks again

-Ali

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: sharing data from a list item

You can fetch the indexPath of first visible item in listView by attaching ListScrollStateHandler to ListView. This page has an example at the top of the page:

http://developer.blackberry.com/cascades/reference/bb__cascades__listscrollstatehandler.html#propert...

Everything else still applies. Smiley Happy


Andrey Fidrya, @zmeyc on twitter
Developer
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

Re: sharing data from a list item

Yikes!!

tried tinkering with "firstVisibleItem()" a few times and the only thing I'd did successfully was make my head explode haha.  I know the solution is probably in the suggestions you so kindly provided, so I will give another crack at it later once I pick up all the pieces Smiley Happy 

 

 

-Ali

 

Developer
Posts: 55
Registered: ‎12-04-2012
My Device: Q10
My Carrier: Verizon

Re: sharing data from a list item

Do you know if there is anyway to invoke 'share' without an actionitem? ie a button? I think that would definitely help

Developer
Posts: 1,524
Registered: ‎12-18-2012
My Device: Z30, Z10 LE, DevAlpha C, PlayBook

Re: sharing data from a list item

[ Edited ]

The following code initiates a sharing in C++. You can declare this function as Q_INVOKABLE, export the class instance to QML, then call the function from button's onClicked handler in QML.

 

The string for sharing can be passed from QML via function parameters.

 

 

void CalculatorPage::share_triggered()
{
        QString displayString = "String to share...";

        InvokeQuery *query = InvokeQuery::create()
            .data(displayString.toUtf8())
            .mimeType("text/plain")
            .invokeActionId("bb.action.SHARE");
        invocation_ = Invocation::create(query)
            .parent(this);
                                                                                         
        QObject::connect(invocation_, SIGNAL(armed()), this, SLOT(invocation_armed()));
        QObject::connect(invocation_, SIGNAL(finished()), invocation_, SLOT(deleteLater()));
}

...

void CalculatorPage::invocation_armed()
{
        qDebug("Invocation armed");
        invocation_->trigger("bb.action.SHARE");
}

invocation_ variable and invocation_armed slot are declared in header file:

protected slots:
void invocation_armed();

protected:
bb::cascades::Invocation *invocation_;

 

I wasn't able to make this work in pure QML.

 


 

Using ActionItem is simpler:

 

In class constructor call:

 

InvokeQuery *query = InvokeQuery::create()
    .mimeType("text/plain")
    .invokeActionId("bb.action.SHARE");

QString data = QString::fromUtf8("Some initial data");
query->setData(data.toUtf8()); // not neccessary I think

shareResultActionItem_ = InvokeActionItem::create(query)
    .title("Share Result");
addAction(shareResultActionItem_, ActionBarPlacement::OnBar /*or InOverflow*/);

QObject::connect(shareResultActionItem_, SIGNAL(triggered()),
 this, SLOT(share_triggered()));

 in share_triggered() update the data with actual text to share:

void UnitConverterPage::share_triggered()
{
        qDebug() << "User has invoked share action\n";
        QString textToShare = "Some text to share fetched from QML component";
        shareResultActionItem_->setData(textToShare.toUtf8());
}

 shareResultActionItem_ is declared in header file as:

protected:
    bb::cascades::InvokeActionItem *shareResultActionItem_;

 


Andrey Fidrya, @zmeyc on twitter