0

I am trying to create an application where a random image is displayed(Working), and the user selects from a dropdown list the displayed image and this is checked to see if it is correct or not

I am not sure how to track the image displayed as I need something to reference in my if statement

See below my attempt but it returns null on the locationOfCorrectAnswer

public class IdentifyTheBrandActivity extends AppCompatActivity implements  AdapterView.OnItemClickListener,
        AdapterView.OnItemSelectedListener { String[] Brands = { "Audi", "Bentley", "BMW" };


    Button SubmitButton;
    ImageView imageView;
    int[] images;

    int chosenCar = 0;
    int locationOfCorrectAnswer;
    String[] answers = new String[3];
    ArrayList<String> carBrands = new ArrayList<>();



    public void brandChosen(View view) {
        if (view.getTag().toString().equals(Integer.toString(locationOfCorrectAnswer))){
            Toast.makeText(getApplicationContext(), "Correct", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Wrong it was " + carBrands.get(chosenCar), Toast.LENGTH_LONG).show();
        }
    }

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

         imageView = findViewById(R.id.RandomImage);
         imageView.setTag("bg");

        carBrands.add("Audi");
        carBrands.add("Bentley");
        carBrands.add("BMW");

        images = new int[]{R.drawable.audi_a3_2010_27_17_200_20_4_77_56_169_21_fwd_5_4_4dr_igm, R.drawable.bentley_bentayga_2017_229_21_600_60_12_78_68_202_12_awd_5_4_suv_cce,
                R.drawable.bmw_6_series_2014_82_18_310_30_6_74_53_192_20_rwd_4_2_convertible_mua};


        Random random = new Random();
        chosenCar = random.nextInt(images.length);

        locationOfCorrectAnswer = random.nextInt(images.length);

        imageView.setBackgroundResource(images[chosenCar]);

        int incorrectAnswerLocation;


         //Get a random between 0 and images.length-1
        //int imageId = (int) (Math.random() * images.length);
        Spinner spin = (Spinner) findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, Brands);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {
        Toast.makeText(getApplicationContext(), "Selected Brand: " + Brands[position]
                ,Toast.LENGTH_SHORT).show();




        /*SubmitButton = findViewById(R.id.SubmitButton);

        SubmitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                // Toast.makeText(getApplicationContext(), "Correct Brand", Toast.LENGTH_SHORT).show();
            }
        });*/
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

I'm not sure why my brandChosen method is returning null? I believe this is the approach that will work

0 Answers0