0

after testing out examples given here and here, I thought I would be able to add a score-point if the user selected the correct drop-down item. (I'm making a simple quiz app that toasts the final score). Obviously this is too advanced for me, but I really don't want to give up and wondered if anyone might have any idea as to what I'm doing wrong please? Here is how I have it set out in the MainActivity.java, with my failed attempts of implementing a way of checking the selected item stripped out:

First I set out the questions:

package com.example.android.arabic;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.os.Bundle;
import android.util.Log;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import com.example.android.arabic.Nature.Nature;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener{

int arabicScore = 0;

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

    // add emoji Q1
    TextView q1 = findViewById(R.id.Q1_question);
    q1.setText(Nature.HONEY_BEE);

                   // add emoji A2_option1
    TextView qa2Option1 = findViewById(R.id.A2_option1);
    qa2Option1.setText(Nature.GOAT);

    // add emoji A2_option2
    TextView qa2Option2 = findViewById(R.id.A2_option2);
    qa2Option2.setText(Nature.BIRD);

    // add emoji A2_option3
    TextView qa2Option3 = findViewById(R.id.A2_option3);
    qa2Option3.setText(Nature.HORSE);


    //Q3 audio
    Button q3Sound = this.findViewById(R.id.Q3_sound);
    final MediaPlayer q3 = MediaPlayer.create(this, R.raw.camel);
    q3Sound.setOnClickListener(new OnClickListener() {

        public void onClick(View v) { q3.start();
        }
    });

    // add emoji Q4_question
    TextView q4 = findViewById(R.id.Q4_question);
    q4.setText(Nature.ANT);


    // Start of spinner Q4
    Spinner a4 = findViewById(R.id.A4);
// Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence>adapter = ArrayAdapter.createFromResource(this,
            R.array.a4_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
    a4.setAdapter(adapter);
    a4.setOnItemSelectedListener(this);


//Q5 audio
    Button q5Sound = this.findViewById(R.id.Q5_sound);
    final MediaPlayer q5 = MediaPlayer.create(this, R.raw.cow_bird);
    q5Sound.setOnClickListener(new OnClickListener() {

            public void onClick(View v) { q5.start();
            }
        });

//A5
    // add emoji A5_option1
    TextView qa5Option1 = findViewById(R.id.A5_option1);
    qa5Option1.setText(Nature.COW);

    // add emoji A5_option2
    TextView qa5Option2 = findViewById(R.id.A5_option2);
    qa5Option2.setText(Nature.PIG);

    // add emoji A5_option3
    TextView qa5Option3 = findViewById(R.id.A5_option3);
    qa5Option3.setText(Nature.SNAKE);

    // add emoji A5_option4
    TextView qa5Option4 = findViewById(R.id.A5_option4);
    qa5Option4.setText(Nature.BIRD);

}

//Display Result
public void results(View view){
    // Check point for Q1
    EditText a1EditText = findViewById(R.id.A1);
    String a1Entry = a1EditText.getText().toString();
    if (a1Entry.contains("نحلة")) {
        arabicScore = arabicScore + 1;
        }


    // Check point for Q2
    RadioButton a2Radio = findViewById(R.id.A2_option2);
    boolean isa2RadioChecked = a2Radio.isChecked();
    if (isa2RadioChecked){
        arabicScore = arabicScore + 1;
    }

    // Check point for Q3
    EditText a3EditText = findViewById(R.id.A3);
    String a3Entry = a3EditText.getText().toString();
    if (a3Entry.contains("جمل")) {
        arabicScore = arabicScore + 1;
    }

    // Check point for Q4



    Toast resultToast = Toast.makeText(this, "You got " + arabicScore + " of 7 questions right.", Toast.LENGTH_LONG);
    resultToast.show();
}

Preview of layout

}

Thank you very much for your advice in advance! I've been stuck on this for months! :'D

  • Where are you overriding the onItemSelectedListener? You need to call setSelected() on your spinner. Get the position by creating an Integer variable and set that variable equal to arrayAdapter.getPosition(arabicScore). Maybe post all your code, might help to better understand what is going on better. – Ben Morris-Rains Oct 09 '18 at 19:52
  • Where is the `onItemSelected()` method to handle the `OnItemSelectedListener` event? – Barns Oct 09 '18 at 19:53
  • Thank you both, I do not have the onItemSelected() method to handle the OnItemSelectedListener event - I've seen different examples of handling this and obviously did not understand how to apply it! - I've added all of my code in java - I just thought it would take up too much space – positive.bunny Oct 09 '18 at 20:42
  • If implementing the `OnItemSelectedListener` is too complicated, you could use an anonymous method instead. – Barns Oct 09 '18 at 20:48

0 Answers0