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
New Contributor
Posts: 9
Registered: ‎06-23-2013
My Device: Z10
My Carrier: giffgaff

indexpath from TextArea in a ListView problem.

Is there a way to get the index path from a text area item in a list view so I can update the datamodel. Can the parent of the textarea be used in someway.
Developer
Posts: 96
Registered: ‎06-05-2012
My Device: Developer
My Carrier: Developer

Re: indexpath from TextArea in a ListView problem.

Hi,

 

Your problem is not clear, if you want to get selected index of list then you can use getSelectedIndex() signal of ListView.

Please clear what is your actual requirement.

 

Thanks!

Developer
Posts: 109
Registered: ‎08-23-2012
My Device: Blackberry Z10
My Carrier: TATA DOCOMO

Re: indexpath from TextArea in a ListView problem.

Its not clear what you exactly want.

First make your problem clear, it will help to understand your requirement.

Dharmender Singh
Highlighted
New Contributor
Posts: 9
Registered: ‎06-23-2013
My Device: Z10
My Carrier: giffgaff

Re: indexpath from TextArea in a ListView problem.

Ok, I've cut out some of my code, hope this helps...


class ApplicationUI : public QObject
{

   Application      *myApp;
   MyDatabase       *myDb;

   Container        *mainContainer;
   ListView         *mainListView;
   Page             *mainPage;
   NavigationPane   *appNavPane;

    // Functions

    ....

   // APP1 VARS

   Page             *app1Page;
   Container        *app1Container;
   ListView         *app1ListView;

};


void ApplicationUI::app1()
{
    app1Page      = new Page();
    app1Container = new Container();
    app1istView   = new ListView();

    ListScrollStateHandler *lssh = new ListScrollStateHandler(app1ListView);
    connect (lssh, SIGNAL (atEndChanged(bool))             , this, SLOT(app1ListAtEnd(bool)));
    connect (lssh, SIGNAL (atBeginningChanged(bool))       , this, SLOT(app1ListAtStart(bool)));

    app1ListView->setListItemProvider (new myListItemProvider());
    app1ListView->setDataModel (app1ListPopulateModel (QDateTime::currentDateTime(), 0));

    app1Container->add (app1ListView);
    app1Page->setContent (app1Container);
    appNavPane->push (app1Page);
}

app1ListPopulateModel () populates the model from the database using the myDb handle.


// myListItemProvider is an instance of this:

MyListItemProvider::MyListItemProvider()
{
}

VisualNode * MyListItemProvider::createItem
(
   ListView       *list,
   const QString  &type
)
{
    if (type.compare(GroupDataModel::Header) == 0)
    {
         return  (new MyListHeader());
    }
    else
    {
         return (new MyListItem());
    }
}

void MyListItemProvider::updateItem
(
   ListView                  *list,
   bb::cascades::VisualNode  *listItem,
   const QString              &type,
   const QVariantList         &indexPath,
   const QVariant             &data
)
{
    // Update the control with correct data.
    QVariantMap map         = data.value<QVariantMap>();

    if (type.compare(GroupDataModel::Header) == 0)
    {
         MyListHeader *myHeader = static_cast<MyListHeader *>(listItem);
         myHeader->updateItem(map["date"].toString());
    }
    else
    {
         MyListItem *myItem = static_cast<MyListItem *>(listItem);
         myItem->updateItem(map["entry"].toString());
    }
}


Then the MyListItem looks like this:

MyListItem::MyListItem(Container *parent) :  CustomControl(parent)
{
  Container  *itemContainer = new Container();
  DockLayout *itemLayout    = new DockLayout();
  itemContainer->setLayout(itemLayout);

  Container *contentContainer = new Container();
  StackLayout *contentLayout = new StackLayout();
  contentLayout->setOrientation(LayoutOrientation::LeftToRight);
  contentContainer->setLeftPadding(0.0f);
  contentContainer->setLayout(contentLayout);

  mItemTextArea = TextArea::create().text(" ");
  mItemTextArea->setVerticalAlignment(VerticalAlignment::Center);
  mItemTextArea->setLeftMargin(0.0f);
  mItemTextArea->textStyle()->setBase(SystemDefaults::TextStyles::bodyText());
  mItemTextArea->textStyle()->setColor(Color::Black);
  mItemTextArea->setPreferredWidth(768.0f);
  mItemTextArea->setMinHeight(200.0f);

  connect (mItemTextArea, SIGNAL(focusedChanged (bool)), this, SLOT(myfocusChanged(bool)));

  contentContainer->add(mItemTextArea);
  itemContainer->add(contentContainer);

  setRoot(itemContainer);
}

void MyListItem::updateItem(const QString text)
{
  mItemTextArea->setText(text);
}

void MyListItem::select(bool select)
{
    qDebug() << "SELECT " << select;
}

void MyListItem::reset(bool selected, bool activated)
{
  Q_UNUSED(activated);
  select(selected);
}

void MyListItem::activate(bool activate)
{
  select(activate);
}

void MyListItem::myfocusChanged(bool focused)
{
    qDebug() << "FOCUSED " << focused;
}



So, when this list is displayed, the values are taken from the database and added to the model whcih is then shown in the listview with each entry populating a textarea.
When I tap to select a row in the listview it sends the textarea into edit mode allowing you to modify its data (which is what I want) and I want to detect the focus out so I'm able to update this row in the database.

I started by trapping the focusedChanged but how can I get the listview row or some database ID from within my focusChanged event?  Also I need access to the myDb pointer in the ApplicationUI. If I passed the pointer into to the ListItemProvider constructor to save internally, would the MyListItem class be able to get hold of it? I wondered if i could use the parent parameter somehow.

Also, when selecting the TextArea withing the ListView, is the row it belongs to also selected? Judging from my lack of debug im getting from the MyListItem::select I presume not. Unless I have it set up incorrectly.


I'm thinking something like:

void MyListItem::myfocusChanged(bool focused)
{
    qDebug() << "FOCUSED " << focused;

     long row_id = ?

     if (!focused)
         myDb->updateApp1Table (row_id, mItemTextArea->text);
}



New Developer
Posts: 11
Registered: ‎01-12-2013
My Device: BB10 Dev Alpha / Playbook/ Z10
My Carrier: Vodafone

Re: indexpath from TextArea in a ListView problem.

Hi

 

I'm not sure if you've since found a solution to your problem.

 

But if you want the indexPath of the currently selected listview item, use

listView->Selected()

 

you can now access each property in the selected item as

QVariantMap map = dataModel->model->data(listView->selected()).toMap();
QString title = map.value("noteName").toString();

etc

 

To update the datamodel, use this to get the index of the item in the datamodel

 

int modelIndex = model->indexOf(listView->Selected(), 0);

 

if (modelIndex > -1)
        {
            model->replace(modelIndex,updatedListItem);
        }

 

Hope this helps.

 

 

Retired
Posts: 83
Registered: ‎10-10-2012
My Device: Z30
My Carrier: Bell

Re: indexpath from TextArea in a ListView problem.

Nostromofire,

 

Did jamaca22 answer your questions?

 

>Is there a way to get the index path from a text area item in a list view so I can update the datamodel?

>Can the parent of the textarea be used in someway?

 

-gary

New Contributor
Posts: 9
Registered: ‎06-23-2013
My Device: Z10
My Carrier: giffgaff

Re: indexpath from TextArea in a ListView problem.

Not really. When my text area was selected within my list I had no way to get access to the list model from the on focus events of the textarea widget. I wanted a way to get the pointer to the my list item provider object from the text area object. They only way I could think of was pass the objects I needed to the constructors and store references to them. Didn't seen right but worked. I encountered other issues along they way so abandoned developing natively and switched to creating a webapp.