0

在此先感谢您的帮助。我想将 CSV 读入数组,然后在 textView 中输出内容。当我在 android 模拟器中运行此代码时,我的 textView 为空。我不知道我做错了什么。文件的读取在带有打印语句的普通 java 中工作。有人可以告诉我这里有什么错误吗?为什么代码没有在 textView 中显示 array[0][1] 内的字符串,是因为我在哪里调用它?我已经尽我所能,但似乎无法让它发挥作用。谢谢一堆。

package com.example.trivia;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.widget.Button;
import android.widget.TextView;

public class PlayActivity extends Activity {
//define buttons and arrays
String [][]array;
TextView timer, questions;
Button answerA, answerB, answerC, answerD;


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    initialize();

    try {
        play();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

      }

public void initialize(){
  timer = (TextView) findViewById(R.id.timer); 
  questions = (TextView) findViewById(R.id.questions);
  answerA = (Button) findViewById(R.id.buttonA);
  answerB = (Button) findViewById(R.id.buttonB);
  answerC = (Button) findViewById(R.id.buttonC);
  answerD = (Button) findViewById(R.id.buttonD);
  array = new String [117][6];
       }

public void play() throws FileNotFoundException{ 
    String delimiter = ",";
    int row =0;
    File file = new File("questions1.csv");
    Scanner sc = new Scanner(file);     

   while (sc.hasNextLine()) 
   {
   String line = sc.nextLine();
   String [] temp = line.split(delimiter);
   for(int i = 0; i< temp.length; i++){
      array[row][i] = temp[i];   
    }
   row++;
   }
   String show = array[0][1];
   questions.setText(show);

 }
}
4

0 回答 0