无法在文本视图中显示文件内容
can not display file content in text view
我正在尝试从警告对话框中 select 一个文件,当我按确定时,我应该使用显示内容方法在文本视图中显示 selected 文件的内容( ) 读取 selected 文件并逐行显示内容,但它不显示任何内容,我认为问题出在调用上,因为我已经在另一个应用程序中使用了该方法并且它有效。
这是我的代码以及我如何调用 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();
}
})
这是 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();
}
替换
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();
}
})
调用另一个 Activity
(startActivityForResult()
) 与 dialog
样式,您将选择 File
和 return 它的路径 Intent
然后在 onActivityResult()
里面你只需要用它来打开 File
然后在 TextView
.
里面显示它的内容
否则你可以先尝试使用create()
然后添加onDismissListener()
。像这样:
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()
我不确定,但这应该可以用 AlertDialog
完成工作,而无需为此创建 Activity
。
这是为了从目录中获取所有文件:
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;
}
您只需发送 myFilesDirectory
作为参数。
如果您 100% 确定所有文件都是文件,而不是目录和 return Array
,您也可以跳过检查 if(!inFile.isDirectory())
。
然后您只需使用 list.get(selectionID)
或 array[selectionID]
获取您的文件,具体取决于您要从方法 return 获得什么。
另外,方法不需要是 static
。我已经 static
满足我的项目需要。
我正在尝试从警告对话框中 select 一个文件,当我按确定时,我应该使用显示内容方法在文本视图中显示 selected 文件的内容( ) 读取 selected 文件并逐行显示内容,但它不显示任何内容,我认为问题出在调用上,因为我已经在另一个应用程序中使用了该方法并且它有效。
这是我的代码以及我如何调用 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();
}
})
这是 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();
}
替换
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();
}
})
调用另一个 Activity
(startActivityForResult()
) 与 dialog
样式,您将选择 File
和 return 它的路径 Intent
然后在 onActivityResult()
里面你只需要用它来打开 File
然后在 TextView
.
否则你可以先尝试使用create()
然后添加onDismissListener()
。像这样:
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()
我不确定,但这应该可以用 AlertDialog
完成工作,而无需为此创建 Activity
。
这是为了从目录中获取所有文件:
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;
}
您只需发送 myFilesDirectory
作为参数。
如果您 100% 确定所有文件都是文件,而不是目录和 return Array
,您也可以跳过检查 if(!inFile.isDirectory())
。
然后您只需使用 list.get(selectionID)
或 array[selectionID]
获取您的文件,具体取决于您要从方法 return 获得什么。
另外,方法不需要是 static
。我已经 static
满足我的项目需要。