重新imageviewer问题

时间:2016-01-10 04:38:37

标签: java codenameone

json中的图像按照预期显示在网格视图中,但是当我单击它们时,只能通过图像查看器刷过2个图像。我不需要上传照片共享演示中的图像我只需要在gridview中显示所有图像,然后在单击时在imageviewer中显示它们。我很难完成这个任务。以下是我到目前为止所做的工作。

ImageGallery表格

@Override
protected void beforeImgGallery(Form f) {
    int iter = 0;
    GridLayout gr = new GridLayout(1, 2);
    Container grid = new Container(gr);
    GridLayout gr = new GridLayout(1, 2);
    grid.setScrollableY(true);
    grid.addComponent(new InfiniteProgress());
    f.addComponent(grid);
    createPictureCommand(grid);
}

connectionRequest - 这里我不需要更新服务器中的图像:

private static boolean animating;
private Vector<Map<String, Object>> responsesgallery;
String[] galleryPhotoUrlCopy = new String[100];
int galleryIndex;

private void createPictureCommand(final Container grid) {
    ConnectionRequest mp = new ConnectionRequest() {
        private long key;

        @Override
        protected void readResponse(InputStream input) throws IOException {
            JSONParser p = new JSONParser();
            results = p.parse(new InputStreamReader(input));
            responsesgallery = (Vector<Map<String, Object>>) results.get("data");
            for (int i = 0; i < responsesgallery.size(); i++) {
                Hashtable hm = (Hashtable) responsesgallery.get(i);
                String galleryImgId = (String) hm.get("news_id");

                String galleryPhotoUrl = (String) hm.get("photo");
                galleryPhotoUrlCopy[i] = galleryPhotoUrl;
                galleryIndex = i;
                key = (long) i;
                long[] img = {key};
                //problem lies in here
                imageList = new ImageList(img);

                final Button btn = createImageButton(key, grid, i, galleryPhotoUrl);
                imageList.addImageId(key);
                grid.addComponent(i, btn);
            }
        }

        @Override
        protected void postResponse() {
        }
    };
    mp.setUrl(SERVER_URL);
    NetworkManager.getInstance().addToQueueAndWait(mp);
}

Command createBackCommand(final Container viewerParent, final Container grid) {
    return new Command("Back") {
        @Override
        public void actionPerformed(ActionEvent evt) {
            viewerParent.getParent().replace(viewerParent, grid, CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 300));
            Form frm = Display.getInstance().getCurrent();
            frm.removeAllCommands();
            frm.setBackCommand(null);
        }
    };
}

imagelist类

public static final String SERVER_URL = "http://capitaleyedevelopment.com/~admin/traffic/api/news/getLatestNews";
private static String THUMB_URL_PREFIX = SERVER_URL + "image?";

class ImageList implements ListModel<Image> {

    private int selection;
    private long[] imageIds;
    private EncodedImage[] images;
    private EventDispatcher listeners = new EventDispatcher();

    public void addImageId(long id) {
        long[] n = new long[imageIds.length + 1];
        EncodedImage[] nImages = new EncodedImage[n.length];
        System.arraycopy(imageIds, 0, n, 0, imageIds.length);
        System.arraycopy(images, 0, nImages, 0, images.length);
        n[imageIds.length] = id;
        imageIds = n;
        images = nImages;
        listeners.fireDataChangeEvent(-1, DataChangedListener.ADDED);
    }

    public long getSelectedImageId() {
        return imageIds[selection];
    }

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

    public Image getItemAt(int index) {
        System.out.println("index " + index);
        System.out.println("images[index] " + images[index]);
        if (images[index] == null) {
            images[index] = placeholder;
            Util.downloadUrlToStorageInBackground(galleryPhotoUrlCopy[index], "FullImage_" + imageIds[index], new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    try {
                        images[index] = EncodedImage.create(Storage.getInstance().createInputStream("FullImage_" + imageIds[index]));
                        listeners.fireDataChangeEvent(index, DataChangedListener.CHANGED);
                        System.out.println("bibek check in ");
                    } catch (IOException err) {
                        err.printStackTrace();
                    }
                }
            });
        }
        //            index = 3;
        return images[index];
    }

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

    public int getSelectedIndex() {
        return selection;
    }

    public void setSelectedIndex(int index) {
        WebServiceProxy.getPhotoLikesAsync(imageIds[selection], new Callback<Integer>() {
            public void onSucess(Integer value) {
                if (likeCount != null) {
                }
            }

            public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
            }
        });
        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) {
    }
}

1 个答案:

答案 0 :(得分:0)

在模型的getSize方法中放置一个断点,并查看返回的数组的长度。我假设它2可以解释你所看到的。

如果您不需要图像ID机制,只需将其替换为其他内容,并将模型映射到您的“真实”数据结构,这是拥有模型的重点。