I am working on a basic file manager and would like to know how i can clear an ArrayAdapter subclass (namely my coustom FileListAdapter) and still get the emptyView to display. here is my current way of making the ArrayAdapter if there are no files:
if(selectedFile.listFiles() == null){
mAdapter = new FileListAdapter(getActivity(), R.layout.file_item_layout, new File[0]);
mListView.setAdapter(mAdapter);
This is the emptyView
mListView.setEmptyView(getActivity().findViewById(android.R.id.empty));
setEmptyText("Empty folder");
Here is my coustom adapter:
public class FileListAdapter extends ArrayAdapter<File> {
private File[] folderContents;
private LayoutInflater inflater;
public FileListAdapter(Context context, int resource, File[] fileList) {
super(context, resource, fileList);
folderContents = fileList;
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = inflater.inflate(R.layout.file_item_layout, parent, false);
}
//find the file
File currentFile = folderContents[position];
//fill the view
ImageView iconImage = (ImageView) itemView.findViewById(R.id.file_icon);
iconImage.setImageResource(FileType.getTypeImage(FileType.parseType(currentFile)));
String fileName = currentFile.getName();
TextView fileNameText = (TextView) itemView.findViewById(R.id.file_name);
fileNameText.setText(fileName);
String fileType = FileType.parse(currentFile);
TextView fileTypeText = (TextView) itemView.findViewById(R.id.file_type);
fileTypeText.setText(fileType);
String fileSizeText;
if(!currentFile.isDirectory()){
long fileSize = currentFile.length();
if(fileSize < 1024){
fileSizeText = fileSize + " Bytes";
} else if(fileSize >= 1024 && fileSize < 1048576){
//fileSizeText = (fileSize/1024) + "KB";
fileSizeText = String.format("%.2f KB", (float)(fileSize/1024.0));
} else if(fileSize >= 1048576 && fileSize < 1073741824){
//fileSizeText = (fileSize/1048576) + "MB";
fileSizeText = String.format("%.2f MB", (float)(fileSize/1048576.0));
} else {
//fileSizeText = (fileSize/1073741824) + "GB";
fileSizeText = String.format("%.2f GB", (float)(fileSize/1073741824.0));
}
}
else{
File[] dirContents = currentFile.listFiles();
if(dirContents == null){
fileSizeText = "N/A";
} else {
if(dirContents.length == 1){
fileSizeText ="1 item";
}
else{
fileSizeText = dirContents.length + " items";
}
}
}
TextView fileSizeTextView = (TextView)itemView.findViewById(R.id.file_size);
fileSizeTextView.setText(fileSizeText);
Date lastModifiedDate = new Date(currentFile.lastModified());
String lastModifiedText = "<" +lastModifiedDate.getMonth() + "/" + lastModifiedDate.getDate() + "/" + (lastModifiedDate.getYear() + 1900) + ">";
TextView lastModifiedDateText = (TextView) itemView.findViewById(R.id.last_modified);
lastModifiedDateText.setText(lastModifiedText);
return itemView;
}
}
Here is the layout for lists (replace listview with gridview to get the other one)
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context="com.pcgeekbrain.madfiles.FileViewFragment">
<ListView android:id="@android:id/list" android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView android:id="@android:id/empty" android:layout_width="match_parent"
android:layout_height="match_parent" android:gravity="center" />
</FrameLayout>
what I get is a blank screen. How can I make the emptyView appear when there is no files in the folder?
Thanks in advance.