2011-03-16 76 views
2

嗨我工作的是一個簡單的文件瀏覽器,是我的應用程序的一部分,我已經添加了一些代碼來刪除文件,但我需要的文件列表後刷新一個項目刪除。這是我到目前爲止的代碼,並且在我撥打電話fileList.notifyDataSetChanged();的地方添加了一條評論,但它對我無效,所以基本上我在這裏做錯了什麼?感謝您的幫助幫助刷新清單與notifyDataSetChanged()

private void getDir(String dirPath) 
{ 

item = new ArrayList<String>(); 
path = new ArrayList<String>(); 

File f = new File(dirPath); 
File[] files = f.listFiles(); 

if(!dirPath.equals(root)) 
{ 

    item.add(root); 
    path.add(root); 

    item.add("../"); 
    path.add(f.getParent()); 

} 

for(int i=0; i < files.length; i++) 
{ 
    File file = files[i]; 
    path.add(file.getPath()); 
    if(file.isDirectory()) 
    item.add(file.getName() + "/"); 
    else 
    item.add(file.getName()); 
} 

ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item); 
setListAdapter(fileList); 
} 

@Override 
protected void onListItemClick(ListView l, View v, final int position, long id) { 

    final File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.boot) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
     final CharSequence[] items = {"Info", "Rename", "Delete"}; 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Options for " + file.getName()); 
     builder.setItems(items, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 
       if (item == 0) 
       { 
        AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this); 
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
        builder.setIcon(R.drawable.info) 
          .setTitle(file.getName() + " info") 
          .setMessage("Path: " + file.getPath() + "\n\nSize: " + (double)file.length()/1024 + " mbs" + "\n\nModified: " + sdf.format(file.lastModified())) 
          .setCancelable(false) 
          .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
           } 
          }).show(); 

       } 
       else if (item == 1) 
       { 
        //TODO Add Rename code here 
        Toast.makeText(Installed.this, "Rename", Toast.LENGTH_SHORT).show(); 
       } 
       else if (item == 2) 
       { 
        AlertDialog.Builder builder = new AlertDialog.Builder(Installed.this); 
        builder.setMessage("Are you sure you want to delete " + file.getName() +"?") 
          .setCancelable(false) 
          .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            File f1 = new File(file.getPath()); 
            boolean success = f1.delete(); 
            if (!success){ 
            Toast.makeText(Installed.this, "Could not delete " + file.getName(), Toast.LENGTH_SHORT).show(); 
            }else{ 
            Toast.makeText(Installed.this, file.getName() + " deleted!", Toast.LENGTH_SHORT).show(); 
            } 
            //This is where I try to refresh the list 
            fileList.notifyDataSetChanged(); 
          } 
          }) 
          .setNegativeButton("No", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            dialog.cancel(); 
           } 
          }).show(); 
       } 
      } 
     }).show(); 
    } 
} 
+2

檢查了這一點: http://stackoverflow.com/questions/4088862/android-list-view-refresh/4088889#4088889閱讀註釋 – blindstuff 2011-03-16 23:43:36

+0

任何一個可以幫助HTTP://計算器。 com/questions/28148618/listview-not-refreshing-after-click-on-button – 2015-01-28 04:34:21

回答

2

刪除item之後,請嘗試使用fileList.remove(theItemYouDeleted)。這應該從Adapter中刪除item。換句話說,您必須從您的數據和adapter中刪除它。 我希望這有助於

+0

我加入這個 'fileList.remove(file.getName());' 到我有這個在我原來的職位,其中 '//這是我嘗試刷新列表 fileList.notifyDataSetChanged(); ' 但是我得到一個錯誤在eclipse中說fileList無法解析,因爲沒有局部變量我猜fileList因爲它在我的onClicks之外?但如果我移動它會導致更多的問題,所以我不明白我該怎麼做 – GFlam 2011-03-16 20:46:10

+1

是的,這是因爲'fileList'不是全局變量,將其定義爲全局變量,或者您可以使用getListAdapter() – raukodraug 2011-03-16 20:59:52

+0

好吧,我加了這個,然後到了我原來的評論'getListAdapter(fileList); \t \t \t \t \t fileList.remove(file.getName());'我仍然得到錯誤關於它沒有解決關於沒有得到這個我可能聽起來很蠢真的很抱歉,但我只是學習就像我說的不到2現在用這個東西現在幾周 – GFlam 2011-03-16 21:51:59

1

對於正常工作,您將需要修改你給適配器列表,並調用notifyDataSetChanged之前刪除被刪除的文件()。

當前您並未從適配器列表中刪除數據。

更新:

當您使用此代碼創建適配器時。

ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.row, item); 

雖然您刪除了該文件,但該列表(代碼中的項目)仍包含有關該文件的數據。您將需要從列表中刪除該數據以查看更新。

+0

你能解釋一下多一點對不起,我對這個整個事情很新,一直用java工作,現在還不到2周 – GFlam 2011-03-16 19:44:16

+0

我該怎麼去關於這樣做? – GFlam 2011-03-16 20:15:15