我想在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();
}
}
重要的是要說清單有她應該有的價值。
有什麼建議嗎? 感謝
你絕對不會在任何地方重新分配列表變量嗎?如果你是這樣會造成這些問題。 – barry 2012-02-06 19:07:09
另一個建議 - 儘量將一些字符串編碼到列表中並通知適配器。這可能是沒有任何東西被添加到列表中,所以你沒有看到任何改變。 – barry 2012-02-06 19:11:44
我不會重新分配清單,但明天我會用你的建議來看看會發生什麼。 – Dporem 2012-02-06 23:10:56