Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8199233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:07:45+00:00 2026-06-07T06:07:45+00:00

I am having the following problem: In my Activity, I have a listview that

  • 0

I am having the following problem:

In my Activity, I have a listview that lists the content of the external files directory.
i want the list to be updated as soon as any kind of change occurs in the content, so I have:

public class FileAdapter implements ListAdapter {
    private File mDirectory;
    private FileObserver mFileObserver;
    private DataSetObservable mDataSetObservable;

    public FileAdapter() {
    super();
    this.mDirectory =
        new ContextWrapper(ImportList.this).getExternalFilesDir(null);
        if (this.mDirectory != null) {
            this.mFileObserver = 
                new FileObserver(this.mDirectory.getAbsolutePath()) {
                    @Override
                    public void onEvent(int event, String path) {
                        new Handler().post(new Runnable() {
                            public void run() {
                                FileAdapter.this.mDataSetObservable.notifyChanged();
                            }
                        });
                    }
                };
        }
        this.mDataSetObservable = new DataSetObservable();
    }
    public void startWatching() {
        this.mFileObserver.startWatching();
    }

    public void stopWatching() {
        this.mFileObserver.stopWatching();
    }

    public boolean areAllItemsEnabled() {
        return true;
    }

    public boolean isEnabled(int position) {
        return true;
    }

    public int getCount() {
        return this.mDirectory == null ? 0 : this.mDirectory.listFiles().length;
    }

    public Object getItem(int arg0) {
        return this.mDirectory.listFiles()[arg0];
    }

    public long getItemId(int position) {
        return position;
    }

    public int getItemViewType(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ItemView itemView;
        if (convertView == null) {
            itemView = new ItemView(ImportList.this, 1);
        } else {
            itemView = (ItemView) convertView;
        }
        File file = (File) this.getItem(position);
        itemView.setValue(0, file.getName());
        return itemView;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public boolean hasStableIds() {
        return false;
    }

    public boolean isEmpty() {
        return this.getCount() < 1;
    }

    public void registerDataSetObserver(DataSetObserver observer) {
        this.mDataSetObservable.registerObserver(observer);
    }

    public void unregisterDataSetObserver(DataSetObserver observer) {
        this.mDataSetObservable.unregisterObserver(observer);
    }

    public String getPath() {
        return this.mDirectory == null ?
            ImportList.this.getResources().getString(R.string.no_external_files) :
            this.mDirectory.getAbsolutePath();
    }
}

as an inner class of my Activity

and in my onCreate I do:

this.mFileBrowser = (ListView) this.findViewById(R.id.listview_filebrowser);
....
this.mFileAdapter = new FileAdapter();
this.mFileBrowser.setAdapter(this.mFileAdapter);

And in onResume() I do:

this.mFileAdapter.startWatching();

So as I understand, this should cause my ListView to automatically reload as soon as a file is deleted from the external files directory, for example.
But it doesn’t work!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:07:46+00:00Added an answer on June 7, 2026 at 6:07 am

    As DJC said, the fileobserver is not recursive. If you do a good search on google, you’ll find a RecursiveFileObserver Class that works very well. One thing I did with it, however, is thread the startWatching method because it takes forever to set up, especially with complex directory trees, so I would advise threading it as to not bind up the UI thread.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having the following problem: I have a ListActivity, its ListView is composed by
I'm having the following problem: I have a ListView and I get the layout
I am having the following problem: a) I have a UNIX build environment set
I have been having the following problem, i think it's probably due to the
I´m having the following problem: I have 2 classes (A and B). The class
I'm currently having the following problem: I've got a TableView that is given TableRow's
I'm having the following problem. I want to write a program in Fortran90 which
I'm having the following problem. I have Visual Web Developer 2010 Express and I'm
I'm having a strange problem with Android 1.6 I have an application that has
I have the following problem, and am having trouble understanding part of the equation:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.