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

Android 删除图像后自动刷新在gridview中不起作用,android,gridview,android-emulator,Android,Gridview,Android Emulator,我正在使用grid view,我是android新手。我之前在gridview中遇到了一个问题,我自己解决了这个问题。我发布了这个链接,因为除了上下文任务中添加的新功能外,代码是相同的 剩下的代码如下所示 @Override public boolean onContextItemSelected(MenuItem item) { if(item.getTitle()=="View"){function1(item.getItemId());} else if(ite

我正在使用grid view,我是android新手。我之前在gridview中遇到了一个问题,我自己解决了这个问题。我发布了这个链接,因为除了上下文任务中添加的新功能外,代码是相同的 剩下的代码如下所示

@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();  
} 

我不允许发布图像,因为我的声誉较低,但当调用“删除”功能时,图像会被删除,但网格视图中有一个空白区域,我希望在调用“删除”功能后自动填充空白区域。

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

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();  
}
公共类ImageAdapter扩展了BaseAdapter{
私有上下文;
ArrayList itemList=新建ArrayList();
公共图像适配器(上下文c){
mContext=c;
}
无效添加(字符串路径){
itemList.add(路径);
}
无效删除(字符串路径){
删除(路径);
}
}
公共无效函数3(int-id){
文件=新文件(路径);
if(file.exists())
{
布尔删除=file.delete();
}
myImageAdapter.remove(路径);
myImageAdapter.notifyDataSetChanged();
Toast.makeText(这个“函数delete called”,Toast.LENGTH_SHORT.show();
}

您需要再次绑定适配器…我不知道如何在函数3中绑定适配器。请告诉代码或任何链接都会有帮助。还有一件事,您知道我在“path”中有图像路径,即/mnt/sdcard/me.jpg我如何获取图像的名称,即“me.jpg”图片的日期请告诉我任何用于此目的的函数或代码;file.getName();//只提供文件名(me.jpg)。