Reputation: 251
I am trying to create a textpad in Android which will load any txt file and show the file. I have used import java.io.File
and tried adding filter by following code but as dir.isDirectory()
always returns true
it add all the files in the list and my filter is not working. Please help if there is any problem with the code or any suggestions can add filter to my file explorer.
public class AndroidExplorer extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root="/";
private TextView myPath;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myPath = (TextView)findViewById(R.id.path);
getDir(root);
}
private void getDir(String dirPath)
{
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
if(dir.isDirectory())
{
System.out.println("%%%%%%%%%%%"+filename);
return true;
}
else
{
System.out.println("*****"+filename);
return filename.endsWith(".txt");
}
}
};
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles(filter);
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead())
getDir(path.get(position));
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
else
{
new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show();
}
}
}
Upvotes: 0
Views: 1872
Reputation: 2656
Your checking if the directory (dir) of the file is a directory in your filter. This will of course always be true. You need to check if the file itself is a directory. You'll have to convert the filename and directory into a new File object and check if that file represents a directory.
I'd recommend using a FileFilter instead of a FilenameFitler. It already has a file object that represents the file being filtered.
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File pathname) {
if(pathname.isDirectory())
{
System.out.println("%%%%%%%%%%%"+pathname);
return true;
}
else
{
System.out.println("*****"+pathname);
return pathname.getName().endsWith(".txt");
}
}
};
Upvotes: 3
Reputation: 10285
If you are filtering TXT files, then why not filter on the extension of the file which is present in the name of the file?
if(dir.getName().endsWith(".txt") {
// This is TXT file
}
EDIT :
To get the directory in which the file is present then use the following line of code in place of //This is TXT file
dir.getParent()
UPDATE :
did you try File.isFile()
?
Upvotes: 0