2012-02-06 90 views
3

我想在android中製作一個文件資源管理器,爲此我有一個帶有listview(下面的代碼)的佈局。通知適配器後,notifyDataSetChanged()不更新ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="500dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:id="@+id/tvfileExplorer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/fileExplorer" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:layout_gravity="center_horizontal"> 
     <Button 
      android:id="@+id/bRoot" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/margins" 
      android:layout_weight=".33" 
      android:text="@string/root" /> 
     <Button 
      android:id="@+id/bOk" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/margins" 
      android:text="@string/ok" 
      android:layout_weight=".33"/> 
     <Button 
      android:id="@+id/bCancel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="@dimen/margins" 
      android:text="@string/cancel" 
      android:layout_weight=".33" /> 
    </LinearLayout>  
    <ListView 
     android:id="@+id/lvFileList" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/margins" 
     android:choiceMode="multipleChoice"> 
    </ListView> 
</LinearLayout> 

後,我的所有文件和文件夾添加到列表中我做出notifyDataSetChanged()到列表中的適配器。但是,listview不會更新。您可以在活動的代碼中看到下面的內容。

public class FileExplorer extends Activity{ 
    private File currentDirectory = new File("/"); 
    private List<String> directoryEntries = new ArrayList<String>(); 
    private ArrayAdapter<String> lvFileExplorerListAdapter; 
    private Activity mActivity; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.file_explorer);   
     mActivity = this; 

     lvFileExplorerListAdapter = new ArrayAdapter<String>(mActivity, android.R.layout.simple_list_item_multiple_choice, directoryEntries); 

     ListView lvFileExplorerList = (ListView)findViewById(R.id.lvFileList); 

     lvFileExplorerList.setAdapter(lvFileExplorerListAdapter);   

     Button bCancel = (Button)findViewById(R.id.bCancel); 
     bCancel.setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) {  
      finish(); 
     } 
    }); 

     Button bRoot = (Button)findViewById(R.id.bRoot); 
     bRoot.setOnClickListener(new OnClickListener() {    
     @Override 
     public void onClick(View v) { 
      browseToRoot(); 
     } 
    }); 

     browseToRoot(); 
    } 

    private void browseToRoot() { 
     browseTo(new File("/"));  
    }  

    private void browseTo(final File aDirectory){ 
    if (aDirectory.isDirectory()){ 
      this.currentDirectory = aDirectory; 
      fill(aDirectory.listFiles()); 
     } 
    } 

    private void fill(File[] files) { 
    directoryEntries.clear(); 

    if(this.currentDirectory.getParent() != null) 
     directoryEntries.add(getString(R.string.upOneLevel)); 

    int currentPathStringLenght = this.currentDirectory.getAbsolutePath().length(); 
    for (File file : files){      
      directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght)); 
    } 

    lvFileExplorerListAdapter.notifyDataSetChanged(); 
    } 
} 

重要的是要說清單有她應該有的價值。

有什麼建議嗎? 感謝

+0

你絕對不會在任何地方重新分配列表變量嗎?如果你是這樣會造成這些問題。 – barry 2012-02-06 19:07:09

+1

另一個建議 - 儘量將一些字符串編碼到列表中並通知適配器。這可能是沒有任何東西被添加到列表中,所以你沒有看到任何改變。 – barry 2012-02-06 19:11:44

+0

我不會重新分配清單,但明天我會用你的建議來看看會發生什麼。 – Dporem 2012-02-06 23:10:56

回答

1

我想通了,我的問題......

上的佈局我有一個包含該按鈕的LinearLayout。就像你上面看到的,我有一個* match_parent *,它可以在列表上面繪製。我把它改成了wrap_content,一切正常。

感謝您的幫助!

0

的問題是在這裏:

directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght)); 

同時適配器保持它自己的數據副本,你將項目添加到directoryEntries。 嘗試更換該行以:

lvFileExplorerListAdapter.add(file.getAbsolutePath().substring(currentPathStringLenght)) ; 

每當你需要改變你有直接的適配器上操作列表項。

+0

對您提出的更改進行了更改,但仍然存在相同的問題。我認爲我的實施是正確的,因爲我以前使用過同樣的方法取得了成功。 – Dporem 2012-02-06 18:16:48

+0

這是不正確的。只要你不重新分配你的列表變量,你可以對它進行操作,而不是對適配器進行操作。 – barry 2012-02-06 19:05:48

+0

@barry可以詳細說明嗎? – Dporem 2012-02-06 22:10:12

0

你試過在notifyDatasetChanged後視圖無效嗎?

lvFileExplorerList.invalidate(); 
+0

是的,仍然是同樣的問題。 – Dporem 2012-02-06 19:06:08

相關問題