簡體   English   中英

在通知適配器后,notifyDataSetChanged()不會更新ListView

[英]notifyDataSetChanged() doesn't update ListView after the adapter being notified

我正在嘗試在android中創建一個文件瀏覽器,為此我有一個帶有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()添加到列表適配器。 但列表視圖不會更新。 您可以在下面看到活動的代碼。

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

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

有什么建議嗎? 謝謝

我想出了我的問題......

在布局上,我有包含按鈕的linearlayout。 就像你在上面看到的那樣,我有一個* match_parent *可以在列表上方繪制。 我將其更改為wrap_content,一切正常。

謝謝你的幫助!

問題出在這里:

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

您正在向directoryEntries添加項目,而適配器保留其自己的數據副本。 嘗試用以下代碼替換該行:

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

每當您需要更改列表項時,您必須直接在適配器上操作。

您是否嘗試過notifyDatasetChanged后視圖無效?

lvFileExplorerList.invalidate();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM