[英]How to sort strings from txt file and post them to textview item
I am reading from txt file some strings.TextView shows me:"score=10" "score=1""score=5""score=11" I want to sort this like:TextView shows:"score=1" "score=5""score=10""score=11" I dont know how to sort this strings.this are not ints. 我正在从txt文件读取一些string.TextView显示给我:“ score = 10”“ score = 1”“ score = 5”“ score = 11”我想像这样进行排序:TextView显示:“ score = 1”“ score = 5“”得分= 10“”得分= 11“我不知道如何对这些字符串进行排序。这不是整数。 Can u Help me with an example ? 你能帮我举个例子吗? Or can u see my code? 还是可以看到我的代码?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wyniki);
final TextView scores = (TextView) findViewById(R.id.textView4);
final Button button5 = (Button) findViewById(R.id.button5);
String saved_scores = readText();
if (saved_scores.length()>0){
scores.setText(saved_scores);}
sort();
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final File f = new File(Environment.getExternalStorageDirectory()+"/cos/Wynik.txt");
if (f.exists()){
f.delete();
Toast.makeText(getApplicationContext(),"Deleted",Toast.LENGTH_SHORT).show();
scores.setText(null);
}
}
});
}
public String readText(){
//this is your text
StringBuilder text = new StringBuilder();
try{
final File f = new File(Environment.getExternalStorageDirectory()+"/cos/Wynik.txt");
FileInputStream fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
String readString = "";
//just reading each line and pass it on the debugger
while((readString = buf.readLine())!= null){
Log.d("line: ", readString);
text.append(readString + "\n");
}
buf.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return text.toString();
} }
Remove the "score=" part of each line in the text file, and for each line check if the remaining content is a valid number - if so, store that as an integer in a collection. 删除文本文件中每一行的“ score =”部分,然后针对每一行检查其余内容是否为有效数字-如果是这样,请将其作为整数存储在集合中。 Once you're done with reading the text file, you can just sort the collection and use the data in whatever fashion you want in your TextView. 阅读完文本文件后,您可以对集合进行排序,并以所需的方式在TextView中使用数据。
You can do the following Scanner Reader=new Scanner(fileName); 您可以执行以下Scanner Reader = new Scanner(fileName); In a for loop read all the integers from using Reader.nextInt() function. 在for循环中,使用Reader.nextInt()函数读取所有整数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.