3

我正在研究网格视图,我是 android 的新手。我之前在 gridview 中遇到了一个问题,我自己解决了我发布链接,因为除了上下文任务中添加的新功能之外,代码是相同的, 我不知道如何获取我单击的 img 的位置,以便我可以将其传递给下一个活动 ,其余代码如下

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

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

4

1 回答 1

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();  
}
于 2013-05-06T08:40:03.903 回答