mrKlar / PagedDragDropGrid

An Android ViewGroup that implements a paged grid with drag'n'drop moveable items
406 stars 184 forks source link

Adding items dynamically is broken after merging #47 #48

Open kamilzych opened 10 years ago

kamilzych commented 10 years ago

After merge #47 that fixed issue #41 I can't add new items after adapter is set. You can simply modify current pageddragdropgrid-examples project to see this issue. Before merging #47 this code was working perfectly

1 Remove adding pages and items from ExamplePagedDragDropGridAdapter constructor 2 Add new method to ExamplePagedDragDropGridAdapter

    public void addItem(final Item item) {
        boolean added = false;
        for (final Page page : pages) {
            if (page.getItems().size() < rowCount() * columnCount()) {
                page.addItem(item);
                added = true;
                break;
            }
        }
        if (!added) {
            final Page page = new Page();
            page.addItem(item);
            pages.add(page);
        }
        gridview.notifyDataSetChanged();
    }

3 Change row count and column count from AUTOMATIC to ex. 3

4 In ExampleActivity at the end of onCreate() method add

adapter.addItem(new Item(1, "item1", R.drawable.ic_launcher));
adapter.addItem(new Item(2, "item2", R.drawable.ic_launcher));

5 Exception is thrown

    java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
            at java.util.ArrayList.get(ArrayList.java:308)
            at ca.laplanete.mobile.pageddragdropgrid.DragDropGrid.layoutAChild(DragDropGrid.java:1005)
            at ca.laplanete.mobile.pageddragdropgrid.DragDropGrid.layoutPage(DragDropGrid.java:993)
            at ca.laplanete.mobile.pageddragdropgrid.DragDropGrid.onLayout(DragDropGrid.java:977)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.HorizontalScrollView.onLayout(HorizontalScrollView.java:1474)
            at ca.laplanete.mobile.pageddragdropgrid.PagedDragDropGrid.onLayout(PagedDragDropGrid.java:234)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:690)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Any idea?

//edit @anlosar Since you are author of KitKat fix, maybe you know how to fix this?

daflip commented 10 years ago

I realise this project isn't maintained anymore however I believe I tracked down this bug.. this is the fix:

https://github.com/daflip/PagedDragDropGrid/commit/0a0dcaf1d22f2af6cf386e01aafa2b46a8af2793