从另一个类访问ArrayList

时间:2012-11-30 15:25:24

标签: java android mobile arraylist

我无法从另一个类访问我的ArrayList。我正在关注过去的例子和互联网示例,但似乎无法让它发挥作用。

我发布了3个课程,只是为了彻底。错误发生在我的QuestionView.java类中。我在我的代码中记录了错误消息的位置和内容。

对我的代码的任何其他部分的任何其他建议表示赞赏。 :)

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 个答案:

答案 0 :(得分:5)

您正在调用getTenQs(),就好像它是ArrayList中的方法一样。我想你想要的是:

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

希望有所帮助!