2013-05-05 50 views
3

我在网格视图上工作,并且我是android.I中的新成员之前在gridview中遇到了一个问题,我解决了自己我发布链接,因为代码与新的在上下文任务附加功能 i dont know how to get the position of the img that i click so i can pass it to next acitvity 剩下的代码如下删除图像后自动刷新不能在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(); 
} 

我不允许张贴图片,因为我有较少的声誉,但是当删除func被称为图像被删除,但有一个空的空间在网格视图中,我希望在调用delete func后自动填充空白空间。

+0

需要适配器再次绑定... – itsrajesh4uguys 2013-05-05 08:56:48

+0

我不知道我怎么可以绑定在功能上3适配器好心告诉代码或任何链接将是有益的 – user2350640 2013-05-05 19:33:25

回答

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

Thnkx很多答案的作品完美的我。 :) – user2350640 2013-05-06 10:15:21

+0

还有一件事,你知道我有“路径”中图像的路径,即/mnt/sdcard/me.jpg我如何获取图像的名称,即“me.jpg”和日期图像请告诉我任何功能或代码为此目的。 – user2350640 2013-05-06 14:24:35

+0

文件文件=新文件(filePath); file.getName(); //只提供文件名(me.jpg)。 – 2013-05-06 18:15:04