ali
ali

Reputation: 199

why i can't access to method from another class

I'm trying to get questions from Firebase and display them in TextView. First, I have GameManager class, in this class I get all questions and add them in 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);
    }

My problem is I can't access to this method generateRandomQuestion. If I want to get the questions dynamically (with Firebase ) I can't access to it, but when I set the questions in static way i can, I don't know why.

Upvotes: 0

Views: 92

Answers (1)

Elroy Jetson
Elroy Jetson

Reputation: 968

If you're trying to call this method from another class it won't work because you made the method private meaning it can only be accessed within the class itself. You can not call this method from another class.

If this method does not need to be private consider changing it to public or protected

This may also be helpful: What is the difference between public, private, and protected?

Upvotes: 1

Related Questions