Use CAknEnumeratedTextPopupSettingItem dynamically at runtime
In an article -- "Create Dynamic Settings Pages", there is a section describing how to "Adding Enumerated Text Item". See it for details.
In this article, I will introduce another way -- creating a CAknEnumeratedTextPopupSettingItem-derived class to use dynamic data at runtime.
An important note: "Setting Item Lis 6" panic will occur when no match found in the EnumeratedTextArray, not zero-based. To demonstrate this, I will use 1,2,10 for the enumeration values, different from those(0,1,10) in the above article. We will see how to fix it also.
Contents[hide] |
Create test data
First we need some test data, and then load the test data when calling CompleteConstructionL() function.
void CMyTextPopupSettingItem::LoadTestDataL()
{
_LIT(KFirst, "first (1)");
_LIT(KSecond, "second (2)");
_LIT(KThird, "third (10)");
// Get the value array
CArrayPtr<CAknEnumeratedText>* texts = this->EnumeratedTextArray();
CArrayPtr<HBufC>* popTextArr = this->PoppedUpTextArray();
// Empty it
if (texts && popTextArr)
{
texts->ResetAndDestroy();
texts->AppendL(new CAknEnumeratedText(1, KFirst().AllocL()));
texts->AppendL(new CAknEnumeratedText(2, KSecond().AllocL()));
texts->AppendL(new CAknEnumeratedText(10, KThird().AllocL()));
popTextArr->ResetAndDestroy();
popTextArr->AppendL(KFirst().AllocL());
popTextArr->AppendL(KSecond().AllocL());
popTextArr->AppendL(KThird().AllocL());
}
}
void CMyTextPopupSettingItem::CompleteConstructionL()
{
CAknEnumeratedTextPopupSettingItem::CompleteConstructionL();
LoadTestDataL(); // overwrite the resource definitions.
}
Check target value
We must check whether the target value, iValue, is in the loaded array or not. If not, it definitely causes a panic -- "Setting Item Lis 6". In that case, we will choose the first item as the default one. See below:
void CMyTextPopupSettingItem::LoadL()
{
CArrayPtr<CAknEnumeratedText>* texts = this->EnumeratedTextArray();
TInt selectedIndex = this->IndexFromValue(iValue);
if (selectedIndex < 0) // no match found.
{
if (texts->Count() > 0)
{
// choose the first item as default one.
CAknEnumeratedText* item = texts->At(0);
// reset external value to the default one.
this->SetExternalValue(item->EnumerationValue());
}
}
CAknEnumeratedTextPopupSettingItem::LoadL();
}
constructor, etc
The constructor of CMyTextPopupSettingItem:
CMyTextPopupSettingItem::CMyTextPopupSettingItem( TInt aIdentifier, TInt& aValue )
: CAknEnumeratedTextPopupSettingItem(aIdentifier, aValue), iValue(aValue)
{
}
The implementation of CreateAndExecuteSettingPageL:
void CMyTextPopupSettingItem::CreateAndExecuteSettingPageL()
{
CAknSettingPage* dlg = CreateSettingPageL();
SetSettingPage( dlg );
SettingPage()->SetSettingPageObserver(this);
SettingPage()->SetSettingTextL(SettingName());
SettingPage()->ExecuteLD(CAknSettingPage::EUpdateWhenChanged);
SetSettingPage(0);
}
The header file
class CMyTextPopupSettingItem : public CAknEnumeratedTextPopupSettingItem
{
public: // Constructor
CMyTextPopupSettingItem( TInt aIdentifier, TInt& aValue );
void LoadL();
protected: // Functions from base classes
void CreateAndExecuteSettingPageL();
void CompleteConstructionL();
private: // custom methods
void LoadTestDataL();
private:
TInt iValue;
};
Use CMyTextPopupSettingItem
In the CreateSettingItemL method of your Setting List container, add some code like this:
case EMyAppItemTest:
{
// iData = 1; // here you can specify the default value to test the class we have just created.
// iData = 0; // This will cause a panic if no fixes in LoadL function.
settingItem = new (ELeave) CMyTextPopupSettingItem(aIdentifier, iData);
break;
}