Switch to next image in ImageViewer every 3 sec

19 views
Skip to first unread message

Fuad Nassar

unread,
Jan 27, 2020, 12:40:10 PM1/27/20
to CodenameOne Discussions
i used ImageViewer in my app and i want to display images for user as automatically change.
my code is 

ImageViewer iv = new ImageViewer(imodel.getItemAt(0));
  new UITimer(() -> {
            if (i < 4) {

                ///... here i should change selected index, how i can do this ?

                i++;
            } else {
                i = 0;
            }
        }).schedule(3000, true, hi);



      class ImageList implements ListModel<Image> {

            String[] images_str;
            private int selection;

//            public void setImages(String[] images_str) {
//                this.images_str = images_str;
//            }
            private Image[] images;
            private EventDispatcher listeners = new EventDispatcher();

            public ImageList(String[] images_str) {
                this.images_str = images_str;
                this.images = new EncodedImage[images_str.length];
            }

            public Image getItemAt(final int index) {
                if (images[index] == null) {
                    images[index] = placeholder;
                    Util.downloadUrlToStorageInBackground(images_str[index], "list" + index, (e) -> {
                        try {
                            images[index] = EncodedImage.create(Storage.getInstance().createInputStream("list" + index)).scaledWidth(Display.getInstance().getDisplayWidth());
                            listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED);
                        } catch (IOException err) {
                            err.printStackTrace();
                        }
                    });
                }
                return images[index];
            }

            public int getSize() {
                return images_str.length;
            }

            public int getSelectedIndex() {
                return selection;
            }

            public void setSelectedIndex(int index) {
                selection = index;
            }

            public void addDataChangedListener(DataChangedListener l) {
                listeners.addListener(l);
            }

            public void removeDataChangedListener(DataChangedListener l) {
                listeners.removeListener(l);
            }

            public void addSelectionListener(SelectionListener l) {
            }

            public void removeSelectionListener(SelectionListener l) {
            }

            public void addItem(Image item) {
            }

            public void removeItem(int index) {
            }
        };



Shai Almog

unread,
Jan 27, 2020, 9:13:18 PM1/27/20
to CodenameOne Discussions
imodel.setSelectedIndex(...)

Fuad Nassar

unread,
Jan 28, 2020, 1:23:54 AM1/28/20
to CodenameOne Discussions
i tried this but does not work in simulator and android device, i can see the change are done on then next image not on display image. 

On Tuesday, January 28, 2020 at 4:13:18 AM UTC+2, Shai Almog wrote:
imodel.setSelectedIndex(...)

Shai Almog

unread,
Jan 28, 2020, 9:52:49 PM1/28/20
to CodenameOne Discussions
You need to implement the event handling code in your model for this to work. Listeners should be registered and then fired when model data changes.
Reply all
Reply to author
Forward
0 new messages