1

I am having trouble with accessing my ArrayList from another class. I am following past examples and internet examples but just cannot seem to get it to work.

I posted 3 of my classes just to be thorough. The error is in my QuestionView.java class. I documented into my code where and what the error message is.

Any other suggestions on any other parts of my code is appreciated. :)

QuestionView.java

public class QuestionView extends Activity {

    Quiz q = new Quiz();
    ArrayList<Question> queries = new ArrayList<Question>();
    queries.getTenQs();  //error here:  "Syntax error on token "getTenQs", Identifier expected after this token"

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.questionviewmain);

    TextView question = (TextView)findViewById(R.id.question);

    Button answer1 = (Button)findViewById(R.id.answer1);
    Button answer2 = (Button)findViewById(R.id.answer2);
    Button answer3 = (Button)findViewById(R.id.answer3);
    Button answer4 = (Button)findViewById(R.id.answer4);

    for(int i = 0; i < 10; i++) {
        question.setText(queries.get(i).getQuery());
        answer1.setText(queries.get(i).getA1());
        answer2.setText(queries.get(i).getA2());
        answer3.setText(queries.get(i).getA3());
        answer4.setText(queries.get(i).getA4());

                    //more code...
    }
}
  }

Quiz.java

  package com.example.test;

  import java.util.ArrayList;

  public class Quiz {
ArrayList<Question> qList = new ArrayList<Question>();
public static ArrayList<Question> tenQs = new ArrayList<Question>(10);

public Quiz() {
    qList.add(new Question("A", "B", "C", "D", 3, "Question 1?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 2?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 3?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 4?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 5?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 6?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 7?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 8?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 9?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 10?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 11?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 12?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 13?"));
    qList.add(new Question("A", "B", "C", "D", 3, "Question 14?"));
}

public void getRandom10() {
    for(int i = 0; i < 10; i++) {
        Question x = qList.get((int) Math.floor((qList.size()+1)*Math.random()));
        if(tenQs.contains(x) == true) {
            i--;
        } else {
            tenQs.add(x);
        }
    }
}

public ArrayList<Question> getTenQs() { 
    return tenQs;
}
  }

Question.java

  package com.example.test;

  public class Question {
String a1;
String a2;
String a3;
String a4;
int correctAnswer;
String query;

public Question() {
}

public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query) {
    this.a1 = a1;
    this.a2 = a2;
    this.a3 = a3;
    this.a4 = a4;
    this.correctAnswer = correctAnswer;
    this.query = query;        
}

public String getA1() { return a1; }
public String getA2() { return a2; }
public String getA3() { return a3; }
public String getA4() { return a4; }
public String getQuery() { return query; }
public int getCorrectAnswer() { return correctAnswer; }
  }

1 Answer 1

5

You're calling getTenQs() as if it's a method in ArrayList. I think what you want is:

Quiz q = new Quiz();
ArrayList<Question> queries = q.getTenQs();  

Hope that helps!

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

1 Comment

Perfect! Works like a beut now. :)

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.