How can I use onItemSelected in Android?

后端 未结 7 2122
忘掉有多难
忘掉有多难 2020-12-03 00:43
package org.example.mbtiapplication;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widge         


        
7条回答
  •  -上瘾入骨i
    2020-12-03 01:17

    You're almost there. As you can see, the onItemSelected will give you a position parameter, you can use this to retrieve the object from your adapter, as in getItemAtPosition(position).

    Example:

    spinner.setOnItemSelectedListener(this);
    
    ...
    
    public void onItemSelected(AdapterView parent, View view, int pos,long id) {
        Toast.makeText(parent.getContext(), 
            "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
    }
    

    This will put a message on screen, with the selected item printed by its toString() method.

提交回复
热议问题