Creating a CAknEnumeratedTextPopupSettingItem-derived class to use dynamic data at runtime

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.

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& aSliderValue );
//      const TDesC& SettingTextL();
//      CFbsBitmap* CreateBitmapL();
        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;
        }

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值