0

Thanks in advance for the help. I want to read a CSV into array and then output contents in textView. When I run this code in the android emulator, my textView is empty. I have no idea what I'm doing wrong. The reading of the file works in normal java with print statements. Can someone tell me what the bug is here? Why the code doesn't show the string inside array[0][1] in the textView, is it because of where I call it? I have tried all I can but can't seem to get it to work. Thanks a bunch.

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

 }
}
cApple
  • 313
  • 1
  • 7
  • 20
  • 1
    where u have created questions1.csv file in SDCARD, in res/assets folder or in res/raw folder ? – ρяσѕρєя K Mar 27 '13 at 18:13
  • I'd recommend setting a breakpoint in your play() method and starting debug mode (left to the "Run" button in eclipse). Look for obvious problems, e.g. a String that is empty although it should not be. If you know what's the problem, it gets much easier to fix it. Edit: Also consider using a ArrayList as in my code example here: http://stackoverflow.com/questions/6057695/how-to-parse-the-csv-file-in-android-application/7779959#7779959 so that you are not limited to exactly 117 lines. – nspo Mar 27 '13 at 18:20

0 Answers0