我正在尝试在安卓中制作一个文件浏览器,我使用了一个带有列表视图的布局(如下代码所示)。
<?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();
}
}
需要强调的是该列表所包含的值应当是正确的。
有任何建议吗? 谢谢