2

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.

Menachem Hornbacher
  • 2,080
  • 2
  • 24
  • 36

3 Answers3

0

UPDATE 3

Put this on your adapter

@Override
public int getCount() {
    return folderContents.length;
}

UPDATE 2

// method setEmptyView on ListView

@android.view.RemotableViewMethod
public void setEmptyView(View emptyView) {
    mEmptyView = emptyView;

    // If not explicitly specified this view is important for accessibility.
    if (emptyView != null
            && emptyView.getImportantForAccessibility() == IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
        emptyView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
    }

    final T adapter = getAdapter();
    final boolean empty = ((adapter == null) || adapter.isEmpty()); // <- see that
    updateEmptyStatus(empty);
}

// meanwhile, on ArrayAdapter...
// adapter.isEmpty returns getCount!

public boolean isEmpty() {
    return getCount() == 0;
}

UPDATE

Try implement this on your FileListAdapter:

public void removeAll() {
    this.folderContents = new File[]{};
}

And turn empty like this:

adapterInstance.removeAll();
adapterInstance.notifyDataChanged();

You need remove the itens ans call notifyDataChanged(). Check this answer for more details.

Community
  • 1
  • 1
0

search for your empty view in fragment like below

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ListView listView = (ListView)view.findViewById(android.R.id.list);
    listView.setEmptyView(view.findViewById(android.R.id.empty));

    ArrayAdapter<Session> adapter1 = new ArrayAdapter<Session>(this, android.R.layout.simple_list_item_1, 
            android.R.id.text1, MainController.getInstanz().getItems());
    listView.setAdapter(adapter1);
}
dora
  • 2,047
  • 3
  • 18
  • 20
0

I believe that there are many places where its activity / fragment may be having trouble, so I'm pasting an example of how it should be implemented an adapter to "listView" and "emptyView"

HomeActivity.java

package br.com.luiskaufmannsilva.examplefilemanager;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class HomeActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);


        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.content, new FileListFragment())
                .commit();
    }




}

FileListFragment.java

package br.com.luiskaufmannsilva.examplefilemanager;


import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.File;


/**
 * A simple {@link Fragment} subclass.
 */
public class FileListFragment extends Fragment {


    private ListView listView = null;
    private FileListAdapter adapter = null;

    public FileListFragment() {}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_file_list, container, false);

        listView = (ListView) view.findViewById(android.R.id.list);

        listView.setEmptyView(view.findViewById(android.R.id.empty));

        final File file = Environment.getExternalStorageDirectory();
        adapter = new FileListAdapter(getActivity().getApplicationContext(), file.listFiles());

        listView.setAdapter(adapter);

        new Handler()
                .postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.clear();
                        adapter.notifyDataSetChanged();

                        new Handler()
                                .postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        adapter.setFiles(file.listFiles());
                                        adapter.notifyDataSetChanged();
                                    }
                                }, 5 * 1000);


                    }
                }, 5 * 1000);

        return view;
    }


    class FileListAdapter extends ArrayAdapter<File>{

        private LayoutInflater inflater;
        private File[] files;

        public FileListAdapter(Context context, File[] objects) {
            super(context, R.layout.file_item, objects);
            this.files = objects;
            this.inflater = LayoutInflater.from(context);
        }

        public void setFiles(File[] files) {
            this.files = files;
        }

        @Override
        public int getCount() {
            return files.length;
        }

        @Override
        public void clear() {
            files = new File[]{};
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = inflater.inflate(R.layout.file_item, parent, false);

            //find the file
            File currentFile = files[position];

            //fill the view
            ImageView iconImage = (ImageView) convertView.findViewById(R.id.file_icon);
            iconImage.setImageResource(R.mipmap.ic_launcher); // do not

            TextView fileNameView = (TextView) convertView.findViewById(R.id.file_name);
            fileNameView.setText(currentFile.getName());

            return convertView;
        }
    }

}

activity_home.xml

<RelativeLayout
    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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".HomeActivity">

    <LinearLayout
        android:orientation="vertical"
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></LinearLayout>

</RelativeLayout>

fragment_file_list.xml

<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="br.com.luiskaufmannsilva.examplefilemanager.FileListFragment">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>

    <TextView
        android:id="@android:id/empty"
        android:text="No itens"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</FrameLayout>

file_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/file_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:textColor="#ff000000"
        android:id="@+id/file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="br.com.luiskaufmannsilva.examplefilemanager" >

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".HomeActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The above example produces this:

initial: initial

past five seconds after 5 seconds

past five seconds again after 5 seconds again