gpt4 book ai didi

android - 删除图像后自动刷新在 gridview 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 12:58:03 25 4
gpt4 key购买 nike

我正在研究 GridView ,我是 android 的新手。我以前在 gridview 中遇到问题,我自己解决了我发布链接是因为代码是相同的,除了上下文任务中添加的新功能 i dont know how to get the position of the img that i click so i can pass it to next acitvity剩下的代码如下

@Override  
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="View"){function1(item.getItemId());}
else if(item.getTitle()=="Details"){function2(item.getItemId());}
else if(item.getTitle()=="Delete"){function3(item.getItemId());}
else {return false;}
return true;
}

public void function1(int id){


//String prompt;
// Sending image id to FullScreenActivity

/*Toast.makeText(getApplicationContext(),
path,
Toast.LENGTH_LONG).show();*/
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
i.putExtra("id", path );
startActivity(i);
}
public void function2(int id){
Toast.makeText(this, "Details func called", Toast.LENGTH_SHORT).show();
}
public void function3(int id){
File file = new File(path);
if(file.exists())
{
boolean deleted = file.delete();
}
myImageAdapter.notifyDataSetChanged();

//adapter.notifyDataSetChanged();

//gv.invalidateViews();


Toast.makeText(this, "function delete called", Toast.LENGTH_SHORT).show();
}

我不允许发布图像,因为我的声誉较低,但是当调用删除函数时图像被删除但 GridView 中有一个空白空间我希望在调用删除函数后自动填充空白空间.

最佳答案

您需要从适配器中的图像列表中删除该文件。否则,文件路径仍在列表中,因此将尝试加载图像并失败,从而导致空白。

public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();

public ImageAdapter(Context c) {
mContext = c;
}

void add(String path){
itemList.add(path);
}

void remove(String path){
itemList.remove(path);
}
}

public void function3(int id){
File file = new File(path);
if(file.exists())
{
boolean deleted = file.delete();
}
myImageAdapter.remove(path);
myImageAdapter.notifyDataSetChanged();

Toast.makeText(this, "function delete called", Toast.LENGTH_SHORT).show();
}

关于android - 删除图像后自动刷新在 gridview 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16382815/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com