Reputation: 113
I'm trying to make a file explorer in android and for that i have a layout with a listview (Code below).
<?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>
After i add all the files and folders to a list i make an notifyDataSetChanged() to the list adapter. But the listview doesn't update. You can see bellow the code of the activity.
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();
}
}
Its important to say that the list have the values that she should have.
Any suggestion? Thanks
Upvotes: 3
Views: 3402
Reputation: 113
I figured out my problem...
On the layout i have the linearlayout that contains the buttons. Like you can see above i have a *match_parent* that was being drawable above the list. I changed it to wrap_content and all is ok.
Thanks for the help guys!
Upvotes: 1
Reputation: 5969
Have you tried invalidate the view after notifyDatasetChanged?
lvFileExplorerList.invalidate();
Upvotes: 0
Reputation: 5173
The problem is here:
directoryEntries.add(file.getAbsolutePath().substring(currentPathStringLenght));
you are adding items to directoryEntries while the adapter keeps its own copy of the data. Try to replace that line with:
lvFileExplorerListAdapter.add(file.getAbsolutePath().substring(currentPathStringLenght)) ;
Whenever you need to change the list items you have to operate directly on the adapter.
Upvotes: 0