Android: two Spinner onItemSelected()

2019-01-14 00:48发布

I have two spinners (day_spin and time_spin) in one Activity. I want to save the selected day_spin value into a variable. Is it possible to differenciate between the two spinners in the same Listener? Or do I have to write my own Listener class for each spinner?

Also I want to get the number of the selected item. For example I have String Array with 6 elements. I select number 3. How can I get the id?

    day_spin = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter_day = ArrayAdapter
            .createFromResource(this, R.array.spinner_day_array,
                    android.R.layout.simple_spinner_item);
    adapter_day
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    day_spin.setAdapter(adapter_day);
    day_spin.setOnItemSelectedListener(this);

    time_spin = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter_time = ArrayAdapter
            .createFromResource(this, R.array.spinner_time_array,
                    android.R.layout.simple_spinner_item);
    adapter_time
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    time_spin.setAdapter(adapter_time);
    time_spin.setOnItemSelectedListener(this);

This is my Listener:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    DAY = parent.getItemAtPosition(pos).toString();
    TIME = parent.getItemAtPosition(pos).toString();
}

I hope you can help me!

4条回答
何必那么认真
2楼-- · 2019-01-14 01:02
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    if(parent.getId() == R.id.spinner1)
    {
      //do this                    
    }
    else if(parent.getId() == R.id.spinner2)
    {
      //do this
    }
}
查看更多
狗以群分
3楼-- · 2019-01-14 01:19

try this...

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if(parent.getId() == R.id.spinnerone)
{
       // operations            
}
else if(parent.getId() == R.id.spinnertwo)
{
  //operations
}
}
查看更多
【Aperson】
4楼-- · 2019-01-14 01:20

in onItemSelected method

int i = spinnerFirst.getSelectedItemPosition();
int j = spinnerSecond.getSelectedItemPosition();

String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];
查看更多
地球回转人心会变
5楼-- · 2019-01-14 01:24
public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    //parent.getItemAtPosition(pos);

    //Spinner spnir = (Spinner)parent;

    parent.getItemAtPosition(pos);

    TextView  tvReadValue = (TextView) findViewById(R.id.tvReadValue);
    switch (parent.getId()) 
    {         
        case R.id.spinDimension:

            tvReadValue.setText("spinDimension" + pos);

            break;              

        case R.id.speedDimension:

            tvReadValue.setText("speedDimension" + pos);
            break;              
    }
查看更多
登录 后发表回答