1

I have this piece of code. I am trying to read elements of the array and display it in a textview in android. The code displays the correct thing if I just use a print statement plain java instead of the android sdk. I've been trying to figure it out for a while now. Any help?

     package com.example.trivia;

      import java.io.File;
      import java.io.IOException;
      import java.util.Random;
      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 {
  String [][]array;
  String [][] questionsArray;
  int []playindexArray = new int[50];
  TextView timer, questions;
  Button answerA, answerB, answerC, answerD;
  String show = "Buttons and Question";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);
    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);



//countdowntimer
    CountDownTimer count = new CountDownTimer(32000, 1000) {
         public void onTick(long millisUntilFinished) {
             timer.setText("TIME REMAINING: " + millisUntilFinished / 1000);

         }
         public void onFinish() {
             timer.setText("TIME UP!");
         }
      };
      count.start();
              try {
        readCSV();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


  public void readCSV() throws IOException{
        array = new String[117][6];
        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++;
         }
        questionsArray = new String[117][6];
        questionsArray = array;
        questions.setText(questionsArray[0][2]);
        }
7
  • 1
    There's no TextView in your code. Show us what you've tried in regards to that. Commented Mar 22, 2013 at 5:28
  • what are you looking for? how to create textview? or how to show in textview? if the later one is what you want then how you want to show in textview? in multi line? Commented Mar 22, 2013 at 5:31
  • @pzyren also, where is your print statement?, show the full and relevant code. Commented Mar 22, 2013 at 5:31
  • Please post your full code... Commented Mar 22, 2013 at 5:40
  • @StinePike the textview is called questions. does not show up in the textview when I run. Commented Mar 22, 2013 at 5:43

1 Answer 1

2

There is no TextView in your code. Nor setText method. The simplest TextView works like this

TextView textView = (TextView) findViewById(R.id.id_of_your_textview);
text.setText("your text");

show the relevant code that you have tried first. then show the error you are getting. this will speed up the process of you getting what you are looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

there are clearly two textViews in there timer and questions refer to class declaration. and there is also the initialization inside OnCreate method.
yes I can see you have updated the code. Now my question is about the readCSV method. from where are you calling it? if this method is supposed to fetch the string you are trying to display then it should be called from somewhere. something like this questions.setText(readCSV(q)); where in you are calling the method and the return type of the method is in the form of a string. something like this. right now i dont see you calling this method from anywhere in this code.
Thank you for taking time to reply. I called it at the end of onCreate. the code itself actually works because if I run it without the SDK it prints what I need. but when I call it in onCreate, it doesn't show up in the textView. I'm not sure If I'm not supposed to be calling it in onCreate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.