Android:从文件读取并增加readed行无法正常工作

时间:2013-01-27 20:45:50

标签: android for-loop readline

我有一个包含不同问题的文件,当用户点击“下一步按钮”时,我需要一个接一个地阅读它们。但是,我只能读取第一行和后一行,在此之后布局保持不变并且不起作用。为了阅读全部问题,我将不胜感激。这是我的代码的一部分:

public class Questions {
int i = 0;
int questionCount = 0;
int category;
String question;
int questionNumber=1;

void currentQuestion(Context context, int cat) {
    category = cat;



    String questionFile = "";



        questionFile = "VerbalSup1.txt";
        questionCount = 25;





    Log.i("Question", questionFile + ": " + questionCount);
    try {
        InputStream is = context.getAssets().open(questionFile);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        // Skips lines
        for (i = 0; i< questionNumber; i++) {
            reader.readLine();

        }
        question = reader.readLine();


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


}
void Next(){
    questionNumber=questionNumber + 1;
}

这是下一个按钮:

bNext.setOnClickListener(new View.OnClickListener(){
            @Override

                public void onClick(View v) {
                    questions.Next();
                     questions = new Questions();
                     questions.Next();
                     currentQuestion();


            }


        });

1 个答案:

答案 0 :(得分:0)

问题是您正在重置questions对象,因此它只会获得第一行。 这里:

questions.Next();
questions = new Questions(); // this is wrong as the counts will be reset to defaults
questions.Next();
currentQuestion();

你应该:

questions.Next();       
currentQuestion();
currentQuestion();

我希望它有所帮助。