【问题标题】:Android: Reading from a file and increasing the readed lines not workingAndroid:从文件中读取并增加读取的行不起作用
【发布时间】:2013-01-27 20:45:49
【问题描述】:

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

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


            }


        });

【问题讨论】:

    标签: android for-loop readline


    【解决方案1】:

    问题是您正在重置 questions 对象,所以它只会得到第一行.. 这里:

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

    你应该有:

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

    希望对你有帮助。

    【讨论】:

    • 欢迎您,我很高兴它有帮助!如果有帮助,您也可以投票作为答案..
    猜你喜欢
    • 2012-09-20
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    相关资源
    最近更新 更多