mercredi 29 juin 2022

How create a random answers in quiz, android studio(java)?

I am tring to do a quiz app, there is a random flag and random 4 answers, one of them should be a correct answer and random 3.

For now I have only been able to random answers, but sometimes I have the same answer several times. Fails to random 3 different answers that will not repeat themselves and add to that the correct answer.

package com.example.guesstheflag;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.media.Image;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class startGame extends AppCompatActivity {

    Integer[] flags ={
            R.drawable.france,
            R.drawable.england,
            R.drawable.brazil,
            R.drawable.belgium,
            R.drawable.germany,
            R.drawable.israel,
            R.drawable.russia,
            R.drawable.spain,
            R.drawable.ukrain,
    };

    String []answers = {
            "france",
            "england",
            "brazil",
            "belgium",
            "germany",
            "israel",
            "russia",
            "spain",
            "ukrain",
    };

    ListView listView;
    ImageView imageView;

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

        listView = findViewById(R.id.myList);
        imageView = findViewById(R.id.imagernd);

        MyAdapter myAdapter = new MyAdapter();
        listView.setAdapter(myAdapter);

        //rnd image
        Random rndImg = new Random();
        int img = rndImg.nextInt(flags.length);
        imageView.setImageResource(flags[img]);
    }

    // functions for answers list
    class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {

            return 4;
        }

        @Override
        public Object getItem(int i) {
            return answers[i];
        }

        @Override
        public long getItemId(int i) {

            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            LayoutInflater layoutInflater = getLayoutInflater();
            View row = layoutInflater.inflate(R.layout.custom, null);

            TextView textView = row.findViewById(R.id.myText);

            Random rnd = new Random();
            int name = rnd.nextInt(answers.length);

            textView.setText(answers[name]);

            return row;
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire