Issue
If i am using Android 2.2 and call File.list()
method in BookGenerator.java, then pages and chapters come in exact sequence, but whenever I execute on Android 4.0 it gives me reverse pages list or reverse pages order.
Is there a compatibility issue between 2.2 and 4.0?
Solution
You shouldn't rely on listFiles() for an ordered list of the pages:
http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#listFiles()
"There is no guarantee that the name strings in the resulting array will appear in any specific order;"
You have to create your own ordering system, based on file name or lastModified or file's size. You can either use a Comparator < File > or Comparator < String > for sorting the files in a SortedSet, or as mentionned earlier, create an own class for the items to sort that implements Comparable. I would suggest the first solution as it's a bit stupid to wrap the File or String class into an other one just for this functionality.
An example with a lot of memory overhead:
TreeSet<File> pages = new TreeSet<File>(new Comparator<File>(){
public int compare(File first, File second) {
return first.getName().compareTo(second.getName());
}
});
for (File file : allFiles) {
pages.add(file());
}
allFiles = pages.toArray();
If you want a more efficient one, you have to implement your own method for sorting an array in its place.
Answered By - Andras Balázs Lajtha
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.