从Android中的TXT文件中读取问题

时间:2017-04-20 19:29:34

标签: java android inputstream

我的应用有一个名为“SituationActivity.java”的java文件。此活动与question.xml相关联。我还有一个名为DAOQuestion.java的DAO类,我从一个名为situation.txt的文本文件中读取信息。我似乎已经完成了所有事情,但当应用程序使用SituationActivity加载到question.xml时,我无法看到文件中的铭文。我想我的问题可能出在我的DAO课程中:

public class DAOQuestion {

Context context;

public DAOQuestion(Context context){
    this.context = context;
}

public ArrayList<Question> getAll() {
    ArrayList<Question> array = new ArrayList<Question>();

    AssetManager am = context.getAssets();

    try {
        InputStream is = am.open("situations.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;

        while ((line = reader.readLine()) != null){
            StringTokenizer st = new StringTokenizer(line, "$");
            while (st.hasMoreTokens()) {
                Question q = new Question();
                q.end = Byte.parseByte(st.nextToken());
                q.situation = st.nextToken();
                q.button1 = st.nextToken();
                q.button2 = st.nextToken();
                q.true1 = Integer.parseInt(st.nextToken());
                q.true2 = Integer.parseInt(st.nextToken());
                array.add(q);

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

此处还有一个来自SituationActivity的部分:

public class SituationActivity extends AppCompatActivity {

TextView situation;
Button button1;
Button button2;
static byte end;
static int true1;
static int true2;
DAOQuestion dao;
Question q;
ArrayList<Question> arr;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.question);

    situation = (TextView) findViewById(R.id.situation);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    dao = new DAOQuestion(this);

    arr = dao.getAll();
    q = arr.get(arr.size() - 1);
    situation.setText(q.situation);
    button1.setText(q.button1);
    button2.setText(q.button2);
    true1 = q.true1;
    true2 = q.true2;
    end = q.end;

来自Question.java的所有内容:

public class Question {
String button1;
String button2;
String situation;
int true1;
int true2;
Byte end = 1;}

关于app:这里是TextView情况,按钮button1和button2。 true1和true2是我应该从哪一行读取的行。因此,situation.txt中的一行是一个布局(就像一个布局,只需转动题字)。问题:我无法从txt文件中读取!!!

0 个答案:

没有答案