语言

Menu
Sites
Language
creating a list items in listview at run time

is it possible to add items to the list at runtime(by a button click) if yes , can any one please guide me by giving an example 

响应

10 回复
Pushpa G

yes. Check this app:On click of Ok button, the list is created and items are added in it

https://developer.tizen.org/sites/default/files/documentation/listviewtest.zip

harish kumar kavali

but in your app list items are fixed, but i want to how to create a append a new item to a list at run time

 

Alex Ashirov

Hello,

It's possible. You need to call RefreshList() function on your list with LIST_REFRESH_TYPE_ITEM_ADD as the second param. Of course, you also need to update your IListViewItemProvider accordingly as well. For example, if you want to add item at the end of the list you need to call something like this on your button press action:

__pList->RefreshList(__count - 1, LIST_REFRESH_TYPE_ITEM_ADD);

Also, IListViewItemProvider::GetItemCount() and IListViewItemProvider::CreateItem() should return appropriate values.

harish kumar kavali

thanks for your suggestion ,
now i am able to items to the list at run time but the "items which are added dynamically are not persisting"
they are deleted when i restart my emulator is there any way to save the items 

harish kumar kavali

my actual problem is, i am able to create the list items at run item but all the items are taking same name ,i need to create the items with different name ,if i changed the item name the change is reflected to all the items but i should reflect only the newly created item  

Alex Ashirov

Hi,

Could you please provide code sample how/where you create items and I will try to figure out what is wrong.

harish kumar kavali


#include "AppResourceId.h"
#include "Listform.h"
#include <FGraphics.h>
#include <FApp.h>
using namespace Tizen::Base;
using namespace Tizen::Ui;
using namespace Tizen::Ui::Scenes;
using namespace Tizen::Ui::Controls;
using namespace Tizen::Graphics;
using namespace Tizen::App;

Listform::Listform(void)
{

}

Listform::~Listform(void)
{
}

bool
Listform::Initialize()
{
    Form::Construct(ListForm);

    return true;
}

result
Listform::OnInitializing(void)
{
    result r = E_SUCCESS;

    // TODO: Add your initialization code here
    AppLog("create main list");
        __pList = new (std::nothrow) ListView();
        __pList->Construct(FloatRectangle(0.0f, 0.0f, GetClientAreaBoundsF().width, GetClientAreaBoundsF().height), true, SCROLL_STYLE_FADE_OUT );
        __pList->SetItemProvider(*this);
        __pList->AddListViewItemEventListener(*this);
        AddControl(__pList);
        Footer *pFooter=GetFooter();
        FooterItem fitem;
        fitem.Construct(508);
        fitem.SetText("Refresh List");
        pFooter->AddItem(fitem);
        pFooter->AddActionEventListener(*this);

    return r;
}

result
Listform::OnTerminating(void)
{
    result r = E_SUCCESS;

    // TODO: Add your termination code here

    return r;
}
void
Listform::OnListViewItemStateChanged(ListView& view, int index, int elementId, ListItemStatus status)
{
    SceneManager* pSceneManager = SceneManager::GetInstance();
    AppAssert(pSceneManager);

    if(status == LIST_ITEM_STATUS_SELECTED)
    {
        switch (index)
        {
        case 0:
            AppLog("in case 0");

            break;
        case 1:
            AppLog("inside case 1");
            break;

        }
    }
}

void
Listform::OnListViewItemSwept(ListView& listView, int index, SweepDirection direction)
{
}

void
Listform::OnListViewContextItemStateChanged(ListView& listView, int index, int elementId, ListContextItemStatus state)
{
}

void
Listform::OnItemReordered(ListView& listView, int oldIndex, int newIndex)
{
}

int
Listform::GetItemCount(void)
{
    return 3;
}

bool
Listform::DeleteItem(int index, ListItemBase* pItem, float itemWidth)
{
    delete pItem;
    pItem = null;
    return true;

}

ListItemBase*
Listform::CreateItem(int index, float itemWidth)
{
    AppLog("in create item list");
    ListAnnexStyle style = LIST_ANNEX_STYLE_NORMAL;
    CustomItem* pItem = new (std::nothrow) CustomItem();
    pItem->Construct(CoordinateSystem::AlignToDevice(Tizen::Graphics::FloatDimension(itemWidth, 112.0f)), style);
    FloatRectangle mainListItemRect(26.0f, 32.0f, GetClientAreaBoundsF().width, 60.0f);

    switch(index)
    {
    case 0:
        AppLog("inside case 0");
        pItem->AddElement(mainListItemRect, ID_FORMAT_STRING, L"Animation", true);
        break;
    case 1:
        AppLog("inside case 1");
        pItem->AddElement(mainListItemRect, ID_FORMAT_STRING, L"Button", true);
        break;

    default:

       break;
    }


    return pItem;
}

void
Listform::OnFormBackRequested(Form& source)
{
    AppLog("pressed back key");
    UiApp* pApp = UiApp::GetInstance();
    AppAssert(pApp);
    pApp->Terminate();
}
void
Listform::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
                                          const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
{
    // TODO:
    // Add your scene activate code here
    AppLog("OnSceneActivatedN");
}

void
Listform::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
                                           const Tizen::Ui::Scenes::SceneId& nextSceneId)
{
    // TODO:
    // Add your scene deactivate code here
    AppLog("OnSceneDeactivated");
}


void
Listform::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
{
    // TODO: Add your implementation codes here
    if(actionId==508)
    {
        __pList->RefreshList(1,LIST_REFRESH_TYPE_ITEM_ADD);

    }

}

Alex Ashirov

Hi,

As I mentioned above you need to change methods inherited from IListViewItemProvider accordingly. Especially, you need to change GetItemCount() and CreateItem(). As we can see from your sample your implementation of the GetItemCount() always returns 3, but it should return actual number of items in the list including dynamically added items. So, if initially you had 3 items and then you have dynamically added 5 items then this function should return 3+5=8. The similar with CreateItem(). Your current implementation has only 2 cases for indexes 0 and 1. But if you actually have more than 2 items including dynamically added you need to change this method accordingly.

harish kumar kavali

Hi thanks for your reply,
 but how to add the cases dynamically 
please help me it is very urgent.

Alex Ashirov

Hi,

Of course, you can’t extend the switch-case statement dynamically. You need to use something else (e.g. loop) This depends on your data-model and where and how you store list items. In general, you need to create and initialize with appropriate values CustomItem for each item you need in the list.