• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Recursive Search not working

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I will start by saying my java is very basic. I have an Android app that is used to display the contents of a certain folder on the device. I have a problem when it comes to search functionality. Currently my user has to select manually which folder to search in so I have multiple activities to display results based on a search filter for each individual folder.

I am not sure why my code doesn't work recursively. When I run it, it just displays a blank screen.

It should show all the files in the folder and subfolders that match the search criteria. I have looked at numerous examples but can't find an answer. What it should do is return an array list of files and when the user clicks on the pdf it opens that file.

my code is:



I really appreciate any feedback as to why you think the recursion isn't working and how I can fix it please?

Thank you
 
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is in the file filter.



searchname is being converted to uppercase, but name remains as it is (probably all lower case). contains() will fail because it does case-sensitive comparison.
Either both should be uppercase or both should be lowercase.

Also a small optimization: Uppercase conversion of searchname is repeated for each and every file in the hierarchy. It can be done just once, by taking it out of accept().
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your reply.

I don't think that is the issue. Even taking out the filename filter it doesn't display files. If I change the path to search so that it is only searching one folder the search works fine.

For example:

I change:



I get results.

The screenshot for the "/RTIO/Operating Procedures" is:






The screenshot for the "/RTIO" path is:





Now if I add some code in to show the folder details to the .isDirectory() function, when I run the app with the "/RTIO" path it shows the folder names:



So it seems that the problem is with the recursion code - it seems to be finding and listing folders but not listing the files within.

Any suggestions?

Thank you very much



 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The change you mentioned regarding the filenamefilter did correct one problem I was having. Previously my search results did not display any titles that were in lowercase, however by changing it to



now displays all results (both lower and uppercase)

Unfortunately it does not fix my recursion problem though.
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel, I took one more look and spot one more problem in the lines:


The "adapter" member should ideally *accumulate* all the matches as the function recurses down the file hierarchy. That means "adapter" should be initialized only once before the recursion starts.
However, in this code, it's getting reset on every entry into fill() to a new FileArrayAdapter object.
For example, if the directory structure is



the flow will be

So you can see from 5) 9) and 14) that adapter got reset multiple times to new FileArrayAdapter objects, but the last object it got reset to was the empty adapter#3. That is what the ListActivity is seeing, hence "\RTIO" is empty.
It's basically discarding all files in subfolders and showing only files under current folder.

When invoked on "RTIO\Operating Procedures", similarly it'll discard all files under subfolders of "RTIO\Operating Procedures" and show only files directly under it.

There are multiple ways of solving this. I've preferred to accumulate the matches in a List<File>. But you can also accumulate it in adapter itself. Just ensure the accumulating field is not initialized multiple times.
Suggested solution:

 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,

Firstly let me say thank you very much for taking the time to go through my code and trouble shoot it - it is very much appreciated. Also, thank you for explaining a bit more on the logic of the Array. I have adapted your response to my code and am very close in having a solution. When I run my search code now and leave the search keyword blank it will now show the search results screen with all of all of the folders in the /RTIO folder. So you have helped me make it search recursively :-)

The only problem is that if I put a keyword in to search, the screen is blank - there are no results???

Here is my new adjusted code with the recursive search working. Can you please have a look and see why you think the filename filter might be causing problems when I put a search term in to check against file names?



I am thinking it has something to do with this code:



That is my filename filter that takes the search term and is run in the private void fill(File f) section. Can you see any reason why the recursion is working with no search term, but when I have a search word put in it returns a blank screen?

Thank you very much for your continuing help.

Kind regards,
Joel
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After further investigation I have found out that the search does work for certain letters or words. If you put in an "a" it will work or some words that are contained in file names will work. So it seems to be having a problem with the logic in searching the titles. Just thought I would add that in case it helps with your reply.
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, here is the FileArrayAdapter.java file:



Perhaps I need to do the filtering in here???
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you are probably right about the file filter.
It'll currently discard subdirectories too which don't contain the keyword. But if I understood your requirement right, you want the code to walk through *all* directories and subdirectories, and filter out only the files.
That is, the filter should apply only for files, not for directories.
So one option is:

 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Yes, I want the code to "walk" through all directories and sub directories and look for file names that contain the search term. I have changed my code to:



However now it seems not to apply the search string at all. It lists every file in all the directories and sub directories - even if there is a search term entered.

Thank you
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joel,
Oops, forgot my accept() API details. My mistake, sorry about that.
In accept(File dir, String name) -> the "dir" is always the parent directory path, not path of current file or subdirectory being evaluated. And "name" is the name of the child file or subdirectory under "dir" that's being evaluated by the filter.
So the full path of current file/subdirectory being evaluated is "dir/name", and this is the one we actually want to check for isDirectory().
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,

Thank you! Thank you! Thank you!

I appreciate all the time and effort you put in to help me fix my recursive/search problems. I have also learned quite a few things regarding arrays etc and the logic behind them also. I have attached my new working code in case it might help someone else at a later date with a similar problem.

Once again, I can't thank you enough and I wish you all the best :-)

Kind regards,
Joel

My corrected code:

 
Karthik Shiraly
Bartender
Posts: 1210
25
Android Python PHP C++ Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome! Glad you got it working
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Karthik,

There is one other feature I just thought of that you may be able to help me with. There is one sub directory that I do not wish to include in the file recursion. That is, I do not want any files from that sub directory to be included in the array.

The sub directory is: File(Environment.getExternalStorageDirectory() + "/RTIO/RecentFiles"));

I am thinking that I need to include something in this section of code:



I have tried a few things but cannot seem to get it to work. Are you able to point me in the right direction please?

Thanks heaps.
 
Joel Keane
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have now solved my problem regarding how to exclude a directory and its files from the search results. FYI the folder that I wanted to exclude was called "Recent Files".

Here is my fixed code:



Hope this helps someone else out.
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic