How can I use onItemSelected in Android?

后端 未结 7 2088
忘掉有多难
忘掉有多难 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条回答
  • 2020-12-03 01:07

    I think this will benefit you Try this I'm using to change the language in my application

    String[] districts;
    Spinner sp;
    

    ......

     sp = (Spinner) findViewById(R.id.sp);
             districts = getResources().getStringArray(R.array.lang_array);
             ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,districts);
             sp.setAdapter(adapter);
             sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                  @Override
                 public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
                      // TODO Auto-generated method stub
                      int index = arg0.getSelectedItemPosition();
                      Toast.makeText(getBaseContext(), "You select "+districts[index]+" id "+position, Toast.LENGTH_LONG).show();
                      switch(position){
                          case 0:
                              setLocal("fr");
                              //recreate();
                              break;
                          case 1:
                              setLocal("ar");
                              //recreate();
                              break;
                          case 2:
                              setLocal("en");
                              //recreate();
                              break;
                          default: //For all other cases, do this
                              setLocal("en");
                              //recreate();
                              break;
                      }
                  }
                 @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                       // TODO Auto-generated method stub
                             }
            });
    

    and this is my String Array

    <string-array name="lang_array">
        <item>french</item>
        <item>arabic</item>
        <item>english</item>
    </string-array>
    
    0 讨论(0)
  • 2020-12-03 01:10

    Another thing: When you have more than one spinner in your layout, you have to implement a switch selection in the onItemSlected() method to know which widget was clicked. Something like this:

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()){
            case R.id.sp_alarmSelection:
                //Do something
                Toast.makeText(this, "Alarm Selected: " + parent.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
                break;
            case R.id.sp_optionSelection:
                //Do another thing 
                Toast.makeText(this, "Option Selected: " + parent.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
                break;
        }
    }
    
    0 讨论(0)
  • 2020-12-03 01:16

    Joseph: spinner.setOnItemSelectedListener(this) should be below Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1); on onCreate

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-03 01:22
    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                   //check if spinner2 has a selected item and show the value in edittext
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                   // sometimes you need nothing here
                }
            });
    
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                   //check if spinner1 has a selected item and show the value in edittext
    
    
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                   // sometimes you need nothing here
                }
            });
    
    0 讨论(0)
  • 2020-12-03 01:24

    If you don't want to implement the listener, you can set it up like this directly where you want it (call on your spinner after your adapter has been set):

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
                    switch (position) {
                        case 0:
                            Toast.makeText(parent.getContext(), "Spinner item 1!", Toast.LENGTH_SHORT).show();
                            break;
                        case 1:
                            Toast.makeText(parent.getContext(), "Spinner item 2!", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(parent.getContext(), "Spinner item 3!", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
    
                @Override
                public void onNothingSelected(AdapterView<?> parent) {
    
                   // sometimes you need nothing here
                }
            });
    
    0 讨论(0)
提交回复
热议问题