Android 从文件排序

Android 从文件排序,android,sorting,Android,Sorting,我想从文件中获取文本,并将其显示在文本视图中 我有一个名为RankActivity的类,其中我使用zapisi()方法将一个字符串和一个名为poenibrojanje的整数写入名为3.txt的文件 public void zapisi() { // WRITING String eol = System.getProperty("line.separator"); String a = txtIme.getText().toString(); try {

我想从文件中获取文本,并将其显示在文本视图中

我有一个名为RankActivity的类,其中我使用zapisi()方法将一个字符串和一个名为poenibrojanje的整数写入名为3.txt的文件

public void zapisi() {
    // WRITING
    String eol = System.getProperty("line.separator");
    String a = txtIme.getText().toString();
    try {
        FileOutputStream fOut = openFileOutput("3.txt", MODE_APPEND);

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        osw.write("Ime: " + a + "  Poeni: " + a1.poenibrojanje + eol);

        // Log.d("Writing", "This is writing log: " + a +
        // +a1.poenibrojanje);

        // osw.flush();
        osw.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
我还有一个类,我从文件中读取内容,并在文本视图中显示。该类称为RankPrikazActivity,下面是完整的代码

包com.test.brzoracunaje

import java.io.BufferedReader;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class RankPrikazActivity extends Activity implements OnClickListener {
TextView tvRank, tvRankPrikaz;
Button btnPovratak;



protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rankprikaz);

    tvRank = (TextView)findViewById(R.id.tvRank);
    tvRankPrikaz = (TextView)findViewById(R.id.tvRankPrikaz);
    btnPovratak = (Button)findViewById(R.id.btnPovratak);
    btnPovratak.setOnClickListener(this);
    citaj();

}


@Override
public void onClick(View v) {

    Intent intent = new Intent(this,PocetnaActivity.class);
    startActivity(intent);


}

public void citaj() {
    // READING
    String eol = System.getProperty("line.separator");
    try {
        BufferedReader input = new BufferedReader(new InputStreamReader(
                openFileInput("3.txt")));
        String line;
        StringBuffer buffer = new StringBuffer();
        while ((line = input.readLine()) != null) {
            buffer.append(line + eol);
        }
        //Log.d("Reading log", "This is reading log:" + buffer);
        //System.out.println(buffer);
        tvRankPrikaz.setText(buffer.toString());

    } catch (Exception e) {
        e.printStackTrace();
    }

}
}

使用该类下名为citaj()方法的方法,我从文件中读取它,并在TextView中显示它,如下所示:

   tvRankPrikaz.setText(buffer.toString());
现在我想在这个文本框中显示它,该文本框按RankActivity类中的整数poenibrojanje排序


如何做到这一点?

最简单的方法是使用a。然后,当您执行select查询时,您可以使用子句对结果进行排序。

简单的做法是使用。然后,在执行select查询时,可以使用子句对结果进行排序。

只需将这些行放在阅读部分,而不是代码中:

StringBuffer buffer = new StringBuffer();
LinkedList<String> strings = new LinkedList<String>();
while ((line = input.readLine()) != null) {
    strings.add(line);
}
Collection.sort(strings);

String text = "";
for(String string : strings) {
    text += string + eol;
}
tvRankPrikaz.setText(text);
StringBuffer=new StringBuffer();
LinkedList字符串=新建LinkedList();
而((line=input.readLine())!=null){
字符串。添加(行);
}
Collection.sort(字符串);
字符串文本=”;
for(字符串:字符串){
文本+=字符串+下线;
}
tvRankPrikaz.setText(文本);

只需将这些行放在阅读部分,而不是代码:

StringBuffer buffer = new StringBuffer();
LinkedList<String> strings = new LinkedList<String>();
while ((line = input.readLine()) != null) {
    strings.add(line);
}
Collection.sort(strings);

String text = "";
for(String string : strings) {
    text += string + eol;
}
tvRankPrikaz.setText(text);
StringBuffer=new StringBuffer();
LinkedList字符串=新建LinkedList();
而((line=input.readLine())!=null){
字符串。添加(行);
}
Collection.sort(字符串);
字符串文本=”;
for(字符串:字符串){
文本+=字符串+下线;
}
tvRankPrikaz.setText(文本);
试试这个:

public void citaj() {
    // READING
    try {
        BufferedReader input = new BufferedReader(new InputStreamReader(
                openFileInput("3.txt")));
        String line;
        TreeMap<Integer,String> sorted_map = new TreeMap<Integer,String>(new Comparator(){
          public int compare(Object o1, Object o2){
            Integer i1 = (Integer)o1;
            Integer i2 = (Integer)o2;
            return -(i1.compareTo(i2));
          }
          public boolean equals(Object o1){
            return this == o1;
          }
        });
        while ((line = input.readLine()) != null) {
          Pattern intsOnly = Pattern.compile("\\d+");
          Matcher makeMatch = intsOnly.matcher(line);
          makeMatch.find();
          Integer inputInt = Integer.valueOf(makeMatch.group());
          sorted_map.put(inputInt, line);
        }
        //Log.d("Reading log", "This is reading log:" + buffer);
        //System.out.println(buffer);
        String toOutput = "";
        for(Integer i: sorted_map.keySet()){
          toOutput += sorted_map.get(i) + "\n";
        }
        tvRankPrikaz.setText(toOutput);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
public void citaj(){
//阅读
试一试{
BufferedReader输入=新的BufferedReader(新的InputStreamReader(
openFileInput(“3.txt”);
弦线;
TreeMap sorted_map=newtreemap(newcomparator(){
公共整数比较(对象o1、对象o2){
整数i1=(整数)o1;
整数i2=(整数)o2;
返回-(i1.与(i2)相比);
}
公共布尔等于(对象o1){
返回此==o1;
}
});
而((line=input.readLine())!=null){
Pattern intsOnly=Pattern.compile(\\d+);
Matcher makeMatch=intsOnly.Matcher(行);
makeMatch.find();
整数输入=Integer.valueOf(makeMatch.group());
已排序的映射put(输入,行);
}
//Log.d(“读取日志”,“这是读取日志:”+缓冲区);
//系统输出打印项次(缓冲区);
字符串toOutput=“”;
for(整数i:sorted_map.keySet()){
toOutput+=已排序的映射。获取(i)+“\n”;
}
tvRankPrikaz.setText(输出);
}捕获(例外e){
e、 printStackTrace();
}
}
试试这个:

public void citaj() {
    // READING
    try {
        BufferedReader input = new BufferedReader(new InputStreamReader(
                openFileInput("3.txt")));
        String line;
        TreeMap<Integer,String> sorted_map = new TreeMap<Integer,String>(new Comparator(){
          public int compare(Object o1, Object o2){
            Integer i1 = (Integer)o1;
            Integer i2 = (Integer)o2;
            return -(i1.compareTo(i2));
          }
          public boolean equals(Object o1){
            return this == o1;
          }
        });
        while ((line = input.readLine()) != null) {
          Pattern intsOnly = Pattern.compile("\\d+");
          Matcher makeMatch = intsOnly.matcher(line);
          makeMatch.find();
          Integer inputInt = Integer.valueOf(makeMatch.group());
          sorted_map.put(inputInt, line);
        }
        //Log.d("Reading log", "This is reading log:" + buffer);
        //System.out.println(buffer);
        String toOutput = "";
        for(Integer i: sorted_map.keySet()){
          toOutput += sorted_map.get(i) + "\n";
        }
        tvRankPrikaz.setText(toOutput);

    } catch (Exception e) {
        e.printStackTrace();
    }

}
public void citaj(){
//阅读
试一试{
BufferedReader输入=新的BufferedReader(新的InputStreamReader(
openFileInput(“3.txt”);
弦线;
TreeMap sorted_map=newtreemap(newcomparator(){
公共整数比较(对象o1、对象o2){
整数i1=(整数)o1;
整数i2=(整数)o2;
返回-(i1.与(i2)相比);
}
公共布尔等于(对象o1){
返回此==o1;
}
});
而((line=input.readLine())!=null){
Pattern intsOnly=Pattern.compile(\\d+);
Matcher makeMatch=intsOnly.Matcher(行);
makeMatch.find();
整数输入=Integer.valueOf(makeMatch.group());
已排序的映射put(输入,行);
}
//Log.d(“读取日志”,“这是读取日志:”+缓冲区);
//系统输出打印项次(缓冲区);
字符串toOutput=“”;
for(整数i:sorted_map.keySet()){
toOutput+=已排序的映射。获取(i)+“\n”;
}
tvRankPrikaz.setText(输出);
}捕获(例外e){
e、 printStackTrace();
}
}

在这里,我编写了一个按数字排序的简单示例

您所要做的就是解析从文件中获取的字符串中的整数,并将其作为映射的键,以及该键的值的字符串(链接)

这是我刚刚写的例子。你需要根据自己的需要进行调整

 Map<Double, String> map = new HashMap<Double, String>();
        map.put(new Double(22.02), "TEST 1");
        map.put(new Double(12.3), "TEST 2");
        map.put(new Double(1.3), "Test 3");
        Set<Double> nums = map.keySet();
       Collection<String> strings = map.values();
        Object[] Arr  = nums.toArray();
        List list= Arrays.asList(Arr);
        Collections.sort(list);
        for(Object o: list)
            map.put((Double) o, map.get(o));


        for(Double d: map.keySet())
            Log.i("TEST", map.get(d));
Map Map=newhashmap();
地图放置(新双(22.02),“测试1”);
map.put(新双精度(12.3),“测试2”);
map.put(新双(1.3),“测试3”);
Set nums=map.keySet();
集合字符串=map.values();
对象[]Arr=nums.toArray();
List=Arrays.asList(Arr);
集合。排序(列表);
用于(对象o:列表)
map.put((双)o,map.get(o));
for(双d:map.keySet())
Log.i(“TEST”,map.get(d));

在这里,我编写了一个按数字排序的简单示例

您所要做的就是解析从文件中获取的字符串中的整数,并将其作为映射的键,以及该键的值的字符串(链接)

这是我刚刚写的例子。你需要根据自己的需要进行调整

 Map<Double, String> map = new HashMap<Double, String>();
        map.put(new Double(22.02), "TEST 1");
        map.put(new Double(12.3), "TEST 2");
        map.put(new Double(1.3), "Test 3");
        Set<Double> nums = map.keySet();
       Collection<String> strings = map.values();
        Object[] Arr  = nums.toArray();
        List list= Arrays.asList(Arr);
        Collections.sort(list);
        for(Object o: list)
            map.put((Double) o, map.get(o));


        for(Double d: map.keySet())
            Log.i("TEST", map.get(d));
Map Map=newhashmap();
地图放置(新双(22.02),“测试1”);
map.put(新双精度(12.3),“测试2”);
map.put(新双(1.3),“测试3”);
Set nums=map.keySet();
集合字符串=map.values();
对象[]Arr=nums.toArray();
List=Arrays.asList(Arr);
集合。排序(列表);
用于(对象o:列表)
map.put((双)o,map.get(o));
for(双d:map.keySet())
Log.i(“TEST”,map.get(d));

根据英语单词进行命名(类、字段、方法等)是一种很好的做法。是的,我现在明白为什么我应该进行英语命名了。在RankActivity类中,是否有按poenibrojanje整数排序的方法,在RankActivity类中,我还将其写入文件,然后读取并保存