jeudi 21 décembre 2017

Android random Fragment load

I am currently programming my first Android app as part of a project at my university. Our group needs some help.

We want to program a game in which you have to ask interactively to answer questions. For this we have considered the following system.

Our questions are loaded from a server as JSON and stored in a list. To load the different questions with the different answer functions we thought we use fragments.

Our current structure looks like this:

-activities
--- GameActivity

-fragment
--- game1 (SimpleTap)
--- game2 (Compass)
--- game3 (Ball)

In the GameActivity the questions and the games(fragments) are loaded. Thereafter, a question is randomly selected and a fragment is randomly assigned and displayed for that question.

At the moment the fragments all looks like this: Example Fragment

The GameActivity looks like this:

public class GameActivity extends FragmentActivity {
private List<Question> questionList;
private Fragment fragmentGame;
private Integer randomQuestion;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    questionList = (List<Question>) getIntent().getSerializableExtra(TopicActivity.QUESTIONS);
    randomQuestion = randomQuestion();

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.game_placeholder, randomGame(), "Game");
    fragmentTransaction.commit();
}

public Integer randomQuestion() {
    int random = ThreadLocalRandom.current().nextInt(0, questionList.size() - 1);
    return random;
}

public Fragment randomGame() {
    List<Fragment> questionGames = new Vector<Fragment>();
    questionGames.add(Fragment.instantiate(this, SimpleTap.class.getName()));
    questionGames.add(Fragment.instantiate(this, Compass.class.getName()));
    Collections.shuffle(questionGames, new Random(System.nanoTime()));
    fragmentGame = questionGames.get(1);
    return fragmentGame;
}

public List<Question> getQuestionList() {
    return questionList;
}

public Integer getRandomQuestion() {
    return randomQuestion;
}
}

One of the Fragments looks like:

public class SimpleTap extends Fragment {

private FragmentActivity listener;
private GameActivity activity;
private List<Question> questionList;
private TextView questionView;
private Button answer1Button, answer2Button, answer3Button, answer4Button;
private TextView answerRView;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof Activity){
        this.listener = (FragmentActivity) context;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.game_simpletap, parent, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    activity = (GameActivity) getActivity();
    questionList = activity.getQuestionList();

    questionView = (TextView) view.findViewById(R.id.question);
    answerRView = (TextView) view.findViewById(R.id.answerR);

    String question = questionList.get(activity.getRandomQuestion()).getQuestion();
    String answer1 = questionList.get(activity.getRandomQuestion()).getAnswer1();
    String answer2 = questionList.get(activity.getRandomQuestion()).getAnswer2();
    String answer3 = questionList.get(activity.getRandomQuestion()).getAnswer3();
    String answer4 = questionList.get(activity.getRandomQuestion()).getAnswer4();

    answer1Button = (Button) view.findViewById(R.id.answer1);
    answer2Button = (Button) view.findViewById(R.id.answer2);
    answer3Button = (Button) view.findViewById(R.id.answer3);
    answer4Button = (Button) view.findViewById(R.id.answer4);

    questionView.setText(question);
    answer1Button.setText(answer1);
    answer2Button.setText(answer2);
    answer3Button.setText(answer3);
    answer4Button.setText(answer4);
}

@Override
public void onDetach() {
    super.onDetach();
    this.listener = null;
}

public void setAnswerR(String answerR){
    answerRView.setText(answerR);
}

public void questionAnswerd(View v) {
    String right = questionList.get(activity.getRandomQuestion()).getAnswerR();
    switch (v.getId()) {
        case R.id.answer1:
            String answer1 = "1";
            if(answer1.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer2:
            String answer2 = "2";
            if(answer2.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer3:
            String answer3 = "3";
            if(answer3.equals(right)){
                setAnswerR("Richtig");
            }
        case R.id.answer4:
            String answer4 = "4";
            if(answer4.equals(right)){
                setAnswerR("Richtig");
            }
        default:
            throw new RuntimeException("Diese Antwort gibt es nicht!");
    }
}
}

So what we need now is a random number generator that both randomly invites random fragments as a game but does not invite them again in one go.

Similarly, we need a query if the question is answered correctly. An approach to this can be seen in the fragment code. However, the output of the response must be passed to the GameActivity for the Random Generator to load a new fragment again.

It's really hard to explain exactly what we want to do because we do not really know if that makes sense at all. Maybe someone will understand us and can help us.

Thanks and best regards




Aucun commentaire:

Enregistrer un commentaire