繁体   English   中英

如何从txt文件中排序字符串并将其发布到textview项目

[英]How to sort strings from txt file and post them to textview item

我正在从txt文件读取一些string.TextView显示给我:“ score = 10”“ score = 1”“ score = 5”“ score = 11”我想像这样进行排序:TextView显示:“ score = 1”“ score = 5“”得分= 10“”得分= 11“我不知道如何对这些字符串进行排序。这不是整数。 你能帮我举个例子吗? 还是可以看到我的代码?

    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();

}

删除文本文件中每一行的“ score =”部分,然后针对每一行检查其余内容是否为有效数字-如果是这样,请将其作为整数存储在集合中。 阅读完文本文件后,您可以对集合进行排序,并以所需的方式在TextView中使用数据。

您可以执行以下Scanner Reader = new Scanner(fileName); 在for循环中,使用Reader.nextInt()函数读取所有整数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM