【发布时间】:2014-11-27 10:39:47
【问题描述】:
我在开发一个项目时使用 Android Eclipse,用户可以在该项目中保存和上传带有图像的笔记。
我有一个片段,其中包含一个文本视图和一些缩略图。用户可以使用相机添加额外的图像,他们可以通过查看(单击)图像(在单独的活动中)并删除它来删除图像。
我的问题是刷新片段的布局以反映已删除的图像。目前正在调用以下函数来处理此问题:
public ViewGroup removeNoteImageFromView(String location) {
List<String> imageLinks = new ArrayList<String>();
for (String string : imageLocations) {
if (string.equalsIgnoreCase(location)) {
break;
}
imageLinks.add(string);
}
//REMOVE ALL THE IMAGEVIEWS from the Linear Layout
LinearLayout linear = (LinearLayout) rootView.findViewById(R.id.imageContainer);
if (linear.getChildCount() != 0) {
linear.removeAllViewsInLayout();
linear.refreshDrawableState();
linear.postInvalidate();
}
imageLocations.addAll(imageLinks);
//Add them all back from the new array
String root = Environment.getExternalStorageDirectory().toString();
for (String string : imageLinks) {
Bitmap myBitmap = BitmapFactory.decodeFile((new File(root +"/saved_images/"+string)).getAbsolutePath());
addImage(myBitmap);
}
return linear;
}
此代码所做的只是隐藏所有由 linear.removeAllViewsInLayout() 调用的缩略图。然而,以下两行是我认为会在屏幕上重新加载布局的内容,但它们似乎没有任何效果。
请注意,我已尝试过 linear.invalidate() 和 postInvalidate,但仍然一无所获。
正确的图像已从设备中删除,因为这是在其他地方处理的,所以当我通过在菜单中重新选择它返回到此片段时,所有内容都会正确显示。
【问题讨论】:
-
这段代码没有什么意义,因为你已经将一个字符串从 imageLocations 添加到 imageLinks 即 imageLinks.add(string);然后再次将相同的字符串读取到 imageLocations imageLocations.addAll(imageLinks);您似乎在阅读之前跳过了清除列表。
-
@humblerookie 问题是我是 android/java 开发的新手,这是我的第一个项目,我之前的一些开发人员已经完成了,所以大部分代码甚至不是我的。害怕:/
-
嘿,我明白了,但是,为什么要从 list(imagelocations) 本身向 List (imagelocations) 添加另一个字符串。我正在反思上面代码使用的这个逻辑。只需通过代码观察您对 imageLocation List 所做的操作
-
这很公平。我想只有以前的开发人员可以回答这个问题哈哈
-
我暗示的是你需要做一个 removeAll() 而不是 addAll() 即 imageLocations.addAll(imageLinks) 到 imageLocations.removeAll(imageLinks) 和 for (String string : imageLinks) 到 for (字符串字符串:imageLocations)
标签: java android eclipse android-fragments view