英文:
How to show the output of script bash in textview as list in java?
问题
以下是您要求的翻译内容:
String[] cmdline = { "sh", "-c", "ls /storage/emulated/0/" }; 
    
try {
    java.lang.Process process = Runtime.getRuntime().exec(cmdline);
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
    StringBuilder result = new StringBuilder();
    String linee = "";
    while ((linee = bufferedReader.readLine()) != null) {
        int count = 1;
        String[] lines = linee.split("\\r?\\n");
    
        for (String line : lines) {
            result.append("line " + count++ + " : " + line + "\n");
        }
    }
    TextView tv = (TextView) findViewById(R.id.textview1);
    tv.setText(result.toString());
    
} catch (Exception s) {
    finishAffinity();
}
这是输出:

请帮我编辑这段代码,以便结果逐行显示。
英文:
I'm using this code to execute bash commands and all is fine in that just when I execute it the displayed output appears like paragraph and not list.
Before it was without line split, I tried to split string lines so below is my result, and this is my code:
String[] cmdline = { "sh", "-c", "ls /storage/emulated/0/" }; 
    
try {
    java.lang.Process process = Runtime.getRuntime().exec(cmdline);
    BufferedReader bufferedReader = new BufferedReader(
            new InputStreamReader(process.getInputStream()));
    StringBuilder result = new StringBuilder();
    String linee = "";
    while ((linee = bufferedReader.readLine()) != null) {
    	int count = 1;
    	String[] lines = linee.split("\\r?\\n");
    
    	for (String line : lines) {
            result.append("line " + count++ + " : " + line);
        }
    }
    TextView tv = (TextView) findViewById(R.id.textview1);
    tv.setText(result.toString());
    
} catch (Exception s) {
    finishAffinity();
}
This is the output:

Please help me edit this code so that the result appears one below one.
答案1
得分: 0
首先:BufferedReader.readLine() 返回一行没有换行符的文本,因此
String[] lines = linee.split("\\r?\\n");
总是返回长度为1的数组,其中包含linee。您应该移除这段代码。
其次:您的行计数器 count 在每次读取一行后都被重置为1,因此无论您读取了多少行,输出始终会包含“line 1:”。
将该计数变量移到 while 循环外以纠正此问题。
第三:您正在将行追加到 result 中,但没有在它们之间添加换行符,因此得到的只是一个单独的段落。
添加换行符(\n)以纠正此问题。
最终,您的代码的核心部分应该如下所示:
StringBuilder result = new StringBuilder();
String line;
int count = 1;
while ((line = bufferedReader.readLine()) != null) {
    result.append("line " + (count++) + " : " + line + "\n");
}
英文:
First: BufferedReader.readLine() returns a single line of text without a line break, so
String[] lines = linee.split("\\r?\\n");
always returns an array of length 1 containing linee. You should remove this code.
Second: your line counter count is reset to 1 after each line read, so your output will always contain "line 1: ", no matter how many lines you read.
Move that count variable outside of the while loop to correct this.
Third: You are appending the lines to result without line breaks between, so all you get is a single paragraph.
Add a line break character ("\n") to correct this.
In the end the central part of your code should look like this:
StringBuilder result=new StringBuilder();
String line;
int count = 1;
while ((line = bufferedReader.readLine()) != null) {
    result.append("line " + (count++) + " : " + line + "\n");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论