来自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);
}
};
}
图像列表类
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) {
}
}
最佳答案
在模型的getSize
方法中放置一个断点,并查看返回的数组的长度。我假设它的2可以解释您所看到的。
如果您不需要图像id机制,只需将其替换为其他内容,然后将模型映射到“真实”数据结构,这就是拥有模型的全部要点。
关于java - 重新imageviewer问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34702116/