Array position determines Toast message

575 views Asked by At

I'm trying to get a Toast to display a message based on a string in an array. I've got two arrays and a listview. When an item in the listview gets selected (which is populated by strings from my first array), I want a toast to come up displaying a message from the second array. The string in position 0 in the first array should correspond to the string in position 0 in the second array.

public class listView_Quiz extends AppCompatActivity {

ListView listView;
ArrayAdapter<String> adapterList;
String [] quizQuestions;
String [] quizAnswers;



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

    listView = (ListView) findViewById(R.id.quizList);
    quizQuestions = getResources().getStringArray(R.array.quizQuestions);
    quizAnswers = getResources().getStringArray(R.array.quizToast);

    adapterList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, quizQuestions) {

    listView.setAdapter(adapterList);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {}

I can get it to half work when I use toString.contains and then write in an answer myself. But I'm looking for a solution where the array position would be checked in each array, and if they match, print the second array message (the answer message)

if (parent.getItemAtPosition(position).toString().contains("Q1")) {
                Toast.makeText(getApplicationContext(), "Answer1", Toast.LENGTH_LONG).show();
            }

            else if (parent.getItemAtPosition(position).toString().contains("Q2")) {
                Toast.makeText(getApplicationContext(), "Answer2", Toast.LENGTH_LONG).show();
            }

As a test, I also also tried some iterations of the below. However, the toast never gets displayed

            String i = quizAnswers[0];
            String j = quizQuestions[0];

            if (parent.getItemAtPosition(position).toString().equals(j)) {
                   Toast.makeText(getApplicationContext(), i, Toast.LENGTH_LONG).show();
                }

Pretty much, what I'm trying to do is

if (selectedListViewitem(position) == Array2(position)) {Toast message}

Is this possible to do? Or will I have to use toString.contains(String)? Thanks

2

There are 2 answers

2
yanivtwin On BEST ANSWER

try

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Toast.makeText(getApplicationContext(), quizAnswers[position], Toast.LENGTH_LONG).show();
}

onitemclicks already gives you the position

hope it helps.

0
amukhachov On

onItemClick method already contains position parameter. You can just take a String from the 2nd array by position and show it with a Toast:

Toast.makeText(listView_Quiz.this, quizAnswers[position], Toast.LENGTH_LONG).show();