文件不会读入多个数组位置

时间:2011-05-11 23:25:57

标签: android arraylist listactivity bufferedreader

我正在尝试将每行从文件存储到arraylist中,然后将两个arraylists合并为一个。目前,当我尝试这个时,所有不同的行都存储在一行中。我想要它说像User:Score。但是,现在它显示为UseruserUsernamePerson:Score。 (许多不同的名字,只有一个分数)。有人能看到我在哪里错了吗?另外,请原谅我糟糕的命名做法。我的数组列表曾经是矢量,但我将它们更改为ArrayLists并忘记更改它们的标题。

public class DisplayScores extends ListActivity{
private ArrayList<String> scoreVector = new ArrayList<String>();
private ArrayList<String> userVector = new ArrayList<String>();
private ArrayList<String> comboVector = new ArrayList<String>();
private int c = 0;
File root = Environment.getExternalStorageDirectory();
File scores = new File(root, "scores.txt");
File users = new File(root, "names.txt");


@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    String line = null;

    try {
        FileReader scoresFileReader = new FileReader(scores);
        BufferedReader scoresReader = new BufferedReader(scoresFileReader);
        while ((line = scoresReader.readLine())!= null) 
        {
            scoreVector.add(line);
        }
        scoresFileReader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String userLine = null;
    try{
        FileReader userFileReader = new FileReader(users);
        BufferedReader userReader = new BufferedReader(userFileReader);
        while((userLine = userReader.readLine())!= null)
        {
            userVector.add(userLine);
        }
        userReader.close();
    } catch (IOException e){
        e.printStackTrace();
    }

    for(String s : scoreVector)
    {
        comboVector.add(userVector.get(c) + ": " + s);
    }

    this.setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, comboVector));

}

}

1 个答案:

答案 0 :(得分:1)

从代码中看来,c的值似乎没有递增.. c总是0

for(String s : scoreVector)
    {
        comboVector.add(userVector.get(c) + ": " + s);
    }