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

时间:2013-05-05 08:37:41

标签: android gridview android-emulator

我正在研究网格视图,我是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();  
} 

我不允许发布图片,因为我的名声较少但是当调用删除功能时图像被删除但网格视图中有一个空白空间我希望在调用删除功能后自动填充空白区域

1 个答案:

答案 0 :(得分:5)

您需要从适配器中的imageList中删除该文件。如果没有这个,文件路径仍然在列表中,因此将尝试加载图像并导致空白空间失败。

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