2013-05-05 74 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