10-23-2012 11:38 AM
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
Solved! Go to Solution.
10-23-2012 11:56 AM
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.
10-23-2012 12:01 PM
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
10-23-2012 05:18 PM
You'll find your answer here https://developer.blackberry.com/cascades/referenc
https://developer.blackberry.com/cascades/referenc
ListItem.indexPath - The index path identifying this item in its DataModel.
https://developer.blackberry.com/cascades/referenc
10-24-2012 07:03 AM
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
10-24-2012 04:22 PM
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
10-25-2012 03:51 AM
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
10-25-2012 08:26 AM - edited 10-25-2012 08:29 AM
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
10-25-2012 02:52 PM