Skip to main content
YCF.1
Associate II
April 19, 2022
Solved

How to get list layout click value

  • April 19, 2022
  • 3 replies
  • 2043 views

I can use listLayout add() item and remove() item good, but I don't know

how to get selected item number on callBackHandler()?

Have any example for me to study ?​

void Screen2View::callBackHandler(CustomContainer2 & cc_1)

{

​

listLayout1.remove(cc_1);

invalidate();

​

}

Thank you & Best regards,

This topic has been closed for replies.
Best answer by Yoann KLEIN

Hello @YCF.1​ ,

A good starting point is to analyze the ListLayout example in TouchGFXDesigner.

You will see in MainView.cpp that a callback "listElementClicked" is implemented. This method will be called every time the user clicks on the button "Delete" and provides the specific CustomListElement object that was clicked. That's already a good way to isolate each element of your list.

Then, if you want to get the specific index of the clicked element of your list, you can try that :

  • Declare an integer variable in CustomListElement.hpp, like this :
int itemIndex;
  • Modify the setupListElement(const Bitmap&,TEXTS) method to include that new parameter.
void setupListElement(const Bitmap& iconBMP, TEXTS iconTextID, int elementIndex);
  • In CustomListElement.cpp, also modify the setupListElement(const Bitmap&,TEXTS) method to allow the user to store the items indexes in this class.
void CustomListElement::setupListElement(const Bitmap& iconBMP, TEXTS iconTextID, int elementIndex)
{
 itemIndex = elementIndex;
 icon.setBitmap(iconBMP);
 text.setTypedText(TypedText(iconTextID));
 text.resizeToCurrentText();
 invalidate();
}
  • Then, in MainView.cpp, fix the call of this function in the setupScreen() method. For instance, there is the function to define the Bitmap and text displayed on the first element. I just defined the element index at "0" here.
listElements[0].setupListElement(Bitmap(BITMAP_ICON0_ID) , T_LIST_ELEMENT_00 , 0);
  • Finally, in the listElementClicked(CustomListElement&) function, you can get the selected item number like this :
int item_nr = element.itemIndex; // "element" is a CustomListElement here

I will also join my project to this post, it might be easier for your understanding.

Let me know if that helped or if you have other questions,

/Yoann

3 replies

Yoann KLEIN
Yoann KLEINBest answer
ST Employee
April 19, 2022

Hello @YCF.1​ ,

A good starting point is to analyze the ListLayout example in TouchGFXDesigner.

You will see in MainView.cpp that a callback "listElementClicked" is implemented. This method will be called every time the user clicks on the button "Delete" and provides the specific CustomListElement object that was clicked. That's already a good way to isolate each element of your list.

Then, if you want to get the specific index of the clicked element of your list, you can try that :

  • Declare an integer variable in CustomListElement.hpp, like this :
int itemIndex;
  • Modify the setupListElement(const Bitmap&,TEXTS) method to include that new parameter.
void setupListElement(const Bitmap& iconBMP, TEXTS iconTextID, int elementIndex);
  • In CustomListElement.cpp, also modify the setupListElement(const Bitmap&,TEXTS) method to allow the user to store the items indexes in this class.
void CustomListElement::setupListElement(const Bitmap& iconBMP, TEXTS iconTextID, int elementIndex)
{
 itemIndex = elementIndex;
 icon.setBitmap(iconBMP);
 text.setTypedText(TypedText(iconTextID));
 text.resizeToCurrentText();
 invalidate();
}
  • Then, in MainView.cpp, fix the call of this function in the setupScreen() method. For instance, there is the function to define the Bitmap and text displayed on the first element. I just defined the element index at "0" here.
listElements[0].setupListElement(Bitmap(BITMAP_ICON0_ID) , T_LIST_ELEMENT_00 , 0);
  • Finally, in the listElementClicked(CustomListElement&) function, you can get the selected item number like this :
int item_nr = element.itemIndex; // "element" is a CustomListElement here

I will also join my project to this post, it might be easier for your understanding.

Let me know if that helped or if you have other questions,

/Yoann

Yoann KLEINST Software Developer | TouchGFX
YCF.1
YCF.1Author
Associate II
April 20, 2022

Hi! Yoann KLEIN

I follow your advice can to get index of item of list Layout.

Thanks!