我正在构建一个用于练习目的的应用程序,所以它就像这样,图像将在gridView中显示在屏幕上,并且: -
以下是应用程序的屏幕截图(到目前为止完成): -
- 如果用户点击图片,则会打开另一个活动,该活动会显示全屏点击的图片。 (完成此部分)
- 如果用户长按图像,则选择图像,用户可以选择多个图像。 (完成此部分)
- 屏幕上有一个菜单,其中包含删除图标/项目和设置为壁纸图标/项目。 (完成此部分)
如果用户点击菜单的删除图标,则应删除所有选定的图像( NOT DONE,我被困在此部分)。
^正如你在这张图片中看到的,我现在选择了两个图像,如果我点击菜单上的RecycleBin图标,它只删除一个图像的起始图像(粉红色),即我试图删除主图像Activity(在这种情况下,它的名字是ImagesShow),然后在循环内或循环外调用notifyDataSetChanged,但它只运行一次并删除一个图像,而在for循环中它检测到当前所选图像数= 2.下面是这部分的代码片段,如果有人需要更详细的代码才能回答,我也可以提供。
//In MainActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.deleteImages:
//Inside RecycleBin Click
//CountSelectedImages simply counts the Number of Images currently Selected
if (CountSelectedImages() > 0)
{
//Images_List is an ArrayList of type Images where Images is an class containing ImagePath as well as boolean condition of IsSelected
for (int x = 0,i=0; x < Images_list.size(); x++)
{
if (Images_list.get(x).IsSelected())
{
//In our above example , we should arive in this location 2 times.
Images_list.remove(x);
//I even tried to write NotifyDataSetChanged here in this if condition just after Removing the Images_List.
//But it still runs one time only , only deleting the first Image that is Selected and never coming back inside this if condition.
ab.setSubtitle(CountSelectedImages() + "/" + Images_list.size() + " items Selected");
}
}
//So i read somewhere that we need to call notifyDataSetChanged once we have done all the changes in the array, so here is my final attempt but still it only runs for one time deleting only single image.
((CustomImageAdapter) gridView.getAdapter()).notifyDataSetChanged(); //Can also write BaseAdapter instead of CustomImageAdapter
ClearWholeSelection(); //This functions clears the Selection of all Images , just as it happens in normal gallery
}//end of if
}//end of switch
}
然而,这有一个小故障,我想如果我试图抓住它我可能会解决问题。
举个例子: - GridView目前包含以下视图: -
现在如果我选择ImageView 0和1并按删除,它只删除0。 但是,如果我按0,2和4,它删除0,2和4(魔术?),即它在这种情况下工作很精细,如果我尝试删除1,3,5 它以某种方式能够多次删除列中的图像,但是当它以行方式删除时,它只删除一个图像。
我试过的其他方法是在CustomImageAdapter中创建Remove(Image)函数,并在上面的for循环中调用,Remove(Images_List.get(x))。 并在删除(图像)我尝试删除CustomImageAdapter列表中的图像(与Images_List相同),然后从CustomImageAdapter调用NotifyDataSetChanged。
Annnnnnd它也没有奏效。
如果有人可以解决这个问题或者在baseAdapter中解释NotifyDataSetChanged的工作,那么请写下你的答案,需要明确我的概念。
还有一件有趣的事情,我在stackoverflow中读到getview()是在一个循环中调用的,这取决于你覆盖的getCount()函数,但是当我在我的Main Activity中写时,它完全让我大吃一惊/ p>
Images_List.get(x).ToggleSelection();
((BaseAdapter)gridView.getAdapter()).notifyDataSetChanged();
它以某种方式成功运行getView并显示&#34; Selected&#34;你可以在上面的屏幕截图中看到图像上的文字,但事实是,计数,即images_list.size()保持不变,那么为什么notifyDataSetChanged在这里成功运行?
P.S对不起长篇帖子:)
EDIT -------------解决
这种情况下的问题是逻辑错误,因为我正在迭代Image_List数组,同时也从中删除它。 感谢Ben的答案(使用Iterator),我的代码现在运行正常。
答案 0 :(得分:1)
问题是当您从列表中删除时,您正在遍历列表。我很惊讶您没有获得并发修改例外。无论如何,如果你想解决这个问题,我建议使用迭代器。
Object
将{{1}}替换为您所发布的代码中未包含的列表类型。
答案 1 :(得分:0)
很抱歉无法评论,所以给出答案本身
def __init__(self, *args, **kwargs):
super(UserFilter, self).__init__(*args, **kwargs)
self.form.initial['archive'] = False
这里的Images_list是什么,位图对象列表或里面包含位图和isSelected布尔值的对象列表?
而且我不认为数组会更好,因为在你的情况下你需要修改很多列表,如果使用数组那么很难。
Images_list.get(x).IsSelected()