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: 36
Registered: ‎09-17-2012
My Device: 9810-Playbooks-devalpha-Z10
My Carrier: Free mobile
Accepted Solution

Listview

Hi,

 

I would like to programatically select the next item in a list view. I've not found a simple way to do that. Is there one?

 

I need to do that in C++, not in QML.

 

Thanks,

 

Fred

Developer
Posts: 6,473
Registered: ‎12-08-2010
My Device: PlayBook, Z10
My Carrier: none

Re: Listview

You could start with ListView.selected(), then adjust the returned QVariantList so that it contains the desired value, then pass it back in to ListView.select() with the second argument true.

 

If you're using a data model with non-simple index paths (multi-level, rootIndexPath not at [], etc) then how you adjust the returned index path may be non-trivial.


Peter Hansen -- (BB10 and dev-related blog posts at http://peterhansen.ca.)
Author of White Noise and Battery Guru for BB10 and for PlayBook | Get more from your battery!
Developer
Posts: 36
Registered: ‎09-17-2012
My Device: 9810-Playbooks-devalpha-Z10
My Carrier: Free mobile

Re: Listview

Hi Peter,

 

This is what I want to do, but I tried to look at the variant to understand what it is, but can't really figure it actually.

 

My data model seems quite simple to me, only one level, a short name, and a url.

 

Do you have any clue?

 

Thanks,

 

Fred

Developer
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon
My Carrier: Play
Developer
Posts: 36
Registered: ‎09-17-2012
My Device: 9810-Playbooks-devalpha-Z10
My Carrier: Free mobile

Re: Listview

I may have not understood, you're giving me the pages I used as references, and I still found nothing that can help.

 

If I get the next value in datamodel, it will not necessarly be the next in the list view given the sort order can be different, so I'm not sure how datamodel will help.

 

If the order is the same, is something like this supposed to work (cause it crashes)?

 

int idx; // idx in model

 

QVariantMap entry;

entry["name"] = mShortName(); // used as title in the ListView
entry["url"] = mUrl; 

idx = m_model->indexOf(entry);

 

idx ++;

 

QVariant v = m_model->value(idx);
QVariantList lv ;
lv.append(v);
mList = mAppPage->findChild<ListView*>("mList");

if ( mList != NULL )
{
mList->clearSelection();
mList->select(lv);
}

 

I'm using indexPath only in qml this is what i use to populate my variables (mUrl and mShortName):

 

onTriggered: {
clearSelection()
select(indexPath)
_trackManager.select( dataModel.data(selected()).url )
}

 

Thanks,

 

Fred

 

Developer
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon
My Carrier: Play

Re: Listview

Hi Fred

 

Can you switch to GroupDataModel ?

 

Q_INVOKABLE QVariantList before (const QVariantList &indexPath) const
Q_INVOKABLE QVariantList after (const QVariantList &indexPath) const

 

BR

Igor

Developer
Posts: 36
Registered: ‎09-17-2012
My Device: 9810-Playbooks-devalpha-Z10
My Carrier: Free mobile

Re: Listview

This would have been a good solution, but unfortunately this is not really an option.

 

I'm using ArrayDataModel so that the user can change the list the way he wants.

 

BTW, I don't understand why the code I wrote to get the next doesn't seem to work:

 

I know the id of the selected object (it' my idx variable), so at the end of this code, I'm supposed to have the next value in vl.

 

Am I right with this?

 

idx++;

QVariant v = model()->value(idx);
QVariantList vl;
vl.clear();
vl.append(v);

 

 

If yes, then as far as I understood what indexPath is, 

this code is supposed to select the item in the list:

 

ListView *list = appPage()->findChild<ListView*>("mList");

if ( list != NULL )
    {
    list->clearSelection();
    list->select(vl);
    }

 

 

Thanks for the help,

 

Fred

Developer
Posts: 36
Registered: ‎09-17-2012
My Device: 9810-Playbooks-devalpha-Z10
My Carrier: Free mobile

Re: Listview

[ Edited ]

Just understood what was wrong with the code, and it was really simple.

 

Here is the code that will work:

 

QVariant vPath = indexPath[0];

int idx = vPath.toInt();

idx++;

QVariant v = idx ; // instead of : v = model()->value(idx);
QVariantList vl; 
vl.append(v);

list->clearSelection();

list->select(vl);
 

I was tring to make things far more complicated than they are, and here I detailed all steps but the code can be even easier.

 

Thanks to those that tried to help me.

 

Fred

 

Developer
Posts: 71
Registered: ‎09-19-2012
My Device: Z10 very soon
My Carrier: Play

Re: Listview

ok I see what you mean

list->clearSelection();
list->select(QVariantList()<<((indexPath[0].toInt())++));
Developer
Posts: 123
Registered: ‎01-11-2013
My Device: BlackBerry z10
My Carrier: Docomo

Re: Listview

Hello Friend,
just use m_model->data(m_model->after(Index));
pass the index from where you want to get the data after. you will get the next item from listview.