Yishai Steinhart
Yishai Steinhart

Reputation: 21

How to refresh a ContainerList

What is the best way to update a ContainerList in runtime. My addPullToRefresh in my ContainerList was just called, I go & fetch new data but how do I refresh the data to show in the ContainerList. How do I force it to rebuild itself with a new model?

I tried rebuilding a new ContainerList and replacing it in the Form, doing it all in a separate thread and doing it within a Display.getInstance().invoke... (tried all types of invoke), but I get a NullPointerException here at

com.codename1.ui.plaf.DefaultLookAndFeel$1.animate(DefaultLookAndFeel.java:1599) at com.codename1.ui.Form.loopAnimations(Form.java:1295) at com.codename1.ui.Form.repaintAnimations(Form.java:1280) at com.codename1.ui.Display.edtLoopImpl(Display.java:1075) at com.codename1.ui.Display.mainEDTLoop(Display.java:994) at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120) at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)


Learned more about the issue, so here is the actual question:

I have a container (WatchList) that has in it a ContainerList (LikeListView). When the ContainerList addPullToRefresh is called it makes a call to contentChanged() on its parent component (WatchList). The contentChanged creates a new LikeListView and replaces the old one with the new one. This is how the code looks like:

public void contentChanged()
    {
    final WatchList self = this;
    Display.getInstance().scheduleBackgroundTask(new Runnable ()
        {
        public void run ()
            {
            CloudData.refreshLikeList();
            Display.getInstance().callSerially(new Runnable() 
                {
                public void run() 
                    {
                    LikeListView lv = new LikeListView (self);
                    self.replace (myListView, lv, null);
                    myListView = lv;
                    }
                });                 
            }
        });
    }

It runs with no problem, but after it is done, there is a NullPointerException

    com.codename1.ui.plaf.DefaultLookAndFeel$1.animate(DefaultLookAndFeel.java:1599)
at com.codename1.ui.Form.loopAnimations(Form.java:1295)
at com.codename1.ui.Form.repaintAnimations(Form.java:1280)
at com.codename1.ui.Display.edtLoopImpl(Display.java:1075)
at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

It looks as if the old LikeListView is being called to animate by the look and feel, and it fails on line 1599 in DefaulrLookAdnFeel

   cmp.getComponentForm().deregisterAnimated(this);

as cmp.getComponentForm() returns null for the removed component.

So, the old component is held somewhere in the DefaultLookAndFeel animated list? How do I get it out of there?

Thanks

-Yishai

Upvotes: 2

Views: 513

Answers (1)

Diamond
Diamond

Reputation: 7483

Make the code that you used to create the initial list model and build ContainerList a method.

If you're using Netbeans, Highlight the block of code -> Right click -> Refactor -> Introduce -> Method and give it a name or press Alt + Shift + M.

Call that method inside the run() method of your addPullToRefresh.

Another thing you can do is call reloadForm();

Upvotes: 1

Related Questions