i am trying to select a file from the alert dialog and when i press OK i should display the content of the selected file in a text View by using the method display Content() that take the selected file read it and display the content line by line, but it does not display anything and i think the problem is with the call because i already use the method in another app and it work.
this is the code i have and how i call displayContent()
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Single Choice List")
.setSingleChoiceItems(files, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectionID = which;
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//display the content of the selected file
displayContent();
Toast.makeText(View_Records.this, "You selected " + files[selectionID], Toast.LENGTH_SHORT).show();
}
})
this is the code for displayContent():
private void displayContent(){
try {
myFilesDirectory = new File(getFilesDir(), "MyFiles");
String fileName = files[selectionID] + ".txt";
File file = new File(myFilesDirectory, fileName);
String text;
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((text = br.readLine()) != null) {
sb.append(text).append("\n");
}
txtViewRecords.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Replace
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setTitle("Single Choice List")
.setSingleChoiceItems(files, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectionID = which;
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//display the content of the selected file
displayContent();
Toast.makeText(View_Records.this, "You selected " + files[selectionID], Toast.LENGTH_SHORT).show();
}
})
with calling for another Activity
(startActivityForResult()
) with dialog
style where you will pick a File
and return it's path with Intent
and then inside onActivityResult()
you just use it to open File
and then display it's content inside TextView
.
Otherwise you can try to use create()
first and add onDismissListener()
. Like this:
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// do you things with builder
AlertDialog ad = builder.create();
ad.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
// here you open file, read content and put it inside TextView`
}
});
ad.show()
I'm not sure, but this should do the job with AlertDialog
without creating Activity
for this.
This is for getting all files from directory:
public static ArrayList<File> listFiles(File folder) {
if (folder.isDirectory()) {
ArrayList<File> tempList = new ArrayList<>();
File[] files = folder.listFiles();
for (File inFile : files) {
if (!inFile.isDirectory()) {
tempList.add(inFile);
}
}
/*Collections.sort(tempList, new Comparator<File>() { //you can sort by name
@Override
public int compare(File file1, File file2) {
return file1.getName().compareTo(file2.getName());
}
});*/
return tempList;
}
return null;
}
And you just send myFilesDirectory
as a parameter.
Also you can skip checking if(!inFile.isDirectory())
if you are 100% sure that all files are files, not directories and return Array
.
And then you just get your file using list.get(selectionID)
or array[selectionID]
depending what you are going to return from method.
Also method doesn't need to be static
. I've got it static
for my project needs.