为什么我无法从另一个类访问方法

时间:2018-03-29 18:20:03

标签: java android class firebase

我试图从Firebase获取问题并将其显示在TextView中。首先,我有GameManager课程,在此class我收到所有问题并将其添加到ArrayList中:

public class GameManager {

    Context context;
    ArrayList<MyQuestion> listquestions = new ArrayList<>();
    private MyQuestion[] questions;
    private String[][] options;
    private int[] answers;
    private DatabaseReference mDatabase;


    public GameManager(Context context) {
        getQuestions();


/*new String[]{"In which country were the first Olympic Games held?", "Which popular american TV show holds the record for most viewers for a finale?", "Where did Chess originate from?", "Which version of Android did not support phones?"};*/
        options = new String[][]{{"Greece", "Spain", "USA", "Russia"},
                {"Cheers", "Friends", "M*A*S*H", "Seinfeld"},
                {"China", "India", "UK", "Germany"},
                {"Donut", "Eclair", "Gingerbread", "Honeycomb"}};
        // 1 - first option, 4 - fourth option
        answers = new int[]{1, 3, 2, 4};
        this.context = context;
        listquestions = new ArrayList<>();
    }

    public QuestionModel generateRandomQuestion(long previousId) {
        Random random = new Random();
        int index = random.nextInt(questions.length);
        while (index == previousId) {
            index = random.nextInt(questions.length);
        }
        QuestionModel testQuestion = new QuestionModel();
        testQuestion.setId(index);
        testQuestion.setQuestion(questions[index].getQuestion());
        List<OptionModel> options = new ArrayList<OptionModel>();

        OptionModel option1 = new OptionModel();
        option1.setOptionId(1);
        option1.setOptionString(this.options[index][0]);
        option1.setCorrectAnswer(answers[index] == 1);

        OptionModel option2 = new OptionModel();
        option2.setOptionId(2);
        option2.setOptionString(this.options[index][1]);
        option2.setCorrectAnswer(answers[index] == 2);
         ....

        options.add(option1);
        options.add(option2);
        .....
        testQuestion.setOptions(options);
        return testQuestion;
    }

    public void getQuestions() {
        mDatabase = FirebaseDatabase.getInstance().getReference();
        final DatabaseReference mUser = mDatabase.child("Quizy").child("ChallengeQuestions").child("Questions");
        mUser.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                    MyQuestion user = postSnapshot.getValue(MyQuestion.class);
                    assert user != null;
                    listquestions.add(user);
                }
                questions = listquestions.toArray(new MyQuestion[listquestions.size()]);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

}


public void generateNewQuestion() {
        long previousQuestionId = -1;
        try {
            previousQuestionId = mGameModel.getQuestion().getId();
        } catch (NullPointerException e) {
            previousQuestionId = -1;
        }
        GameManager gameManager = new GameManager(getApplicationContext());
        mGameModel.setQuestion(gameManager.generateRandomQuestion(previousQuestionId));
        mGameModel.getGameMaster().setDidPlayerAnswer(false);
        mGameModel.getGameMaster().setPlayerAnswerCorrect(false);
        mGameModel.getGameSlave().setDidPlayerAnswer(false);
        mGameModel.getGameSlave().setPlayerAnswerCorrect(false);
    }

我的问题是我无法访问此方法 generateRandomQuestion 。 如果我想动态地提出问题(使用Firebase)我无法访问它,但当我以static方式设置问题时,我不知道为什么

1 个答案:

答案 0 :(得分:1)

如果您尝试从另一个类调用此方法,那么它将无法工作,因为您创建了方法private,这意味着它只能在类本身中访问。你不能从另一个类调用这个方法。

如果此方法不需要private,请考虑将其更改为publicprotected

这可能也会有所帮助:What is the difference between public, private, and protected?