Android:两个 Spinner onItemSelected()

Posted

技术标签:

【中文标题】Android:两个 Spinner onItemSelected()【英文标题】:Android: two Spinner onItemSelected() 【发布时间】:2012-03-04 23:50:50 【问题描述】:

我在一个 Activity 中有两个微调器(day_spin 和 time_spin)。我想将选定的 day_spin 值保存到一个变量中。是否可以区分同一 Listener 中的两个微调器?还是我必须为每个微调器编写自己的 Listener 类?

我还想获取所选项目的编号。例如,我有 6 个元素的字符串数组。我选择了数字 3。如何获取 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);

这是我的Listener

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

希望你能帮帮我!

【问题讨论】:

【参考方案1】:

onItemSelected方法中

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

String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];

【讨论】:

【参考方案2】:
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
    

【讨论】:

@Yaqub 我们可以直接使用getId() 调用而不是类型转换。例如,if(parent.getId() == spinnerView.getId()) ... 请问你是这个意思吗:``` if(parent.getId() == R.id.spinner1)``` @Cabezas 在 2 个条件下使用 switch 是矫枉过正 :)【参考方案3】:
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;              
    

【讨论】:

【参考方案4】:

试试这个...

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


【讨论】:

【参考方案5】:
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) 

    if (adapterView.getId() == R.id.number_of_poket_spinner) 
        NumberOfPokets item = (NumberOfPokets) adapterView.getSelectedItem();
        Toast.makeText(this, item.getPokets(), Toast.LENGTH_LONG).show();
     else if (adapterView.getId() == R.id.select_pleats) 
        PleatsModel item2 = (PleatsModel) adapterView.getSelectedItem();
        Toast.makeText(this, item2.getPleats(), Toast.LENGTH_LONG).show();
    

【讨论】:

【参考方案6】:

同一类的两个 Spinner

我尝试了很多,但最后,我得到了一个有效的代码。

    state = findViewById(R.id.spinnerState);
    country = findViewById(R.id.spinnerCountry);

    List<String> categories = new ArrayList<String>();
    categories.add("Select State");
    categories.add("Andhra Pradesh");
    categories.add("Arunachal Pradesh");
    categories.add("Assam");
    categories.add("Bihar");
    categories.add("Chandigarh");
    categories.add("Delhi");
    categories.add("Goa");
    categories.add("Gujarat");
    categories.add("Haryana");
    categories.add("Himachal Pradesh");
    categories.add("Jammu and Kashmir union territory");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    state.setAdapter(dataAdapter);
    state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
            String item = parent.getItemAtPosition(position).toString();
            Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
        

        @Override
        public void onNothingSelected(AdapterView<?> parent) 
            // can leave this empty
        
    );
    List<String> categories2 = new ArrayList<String>();
    categories2.add("Select Country");
    categories2.add("Afghanistan");
    categories2.add("Albania");
    categories2.add("Algeria");
    categories2.add("India");
    categories2.add("Andorra");
    categories2.add("Angola");
    categories2.add("Antigua and Barbuda");
    categories2.add("Argentina");
    categories2.add("Armenia");
    // Div Spinner implementing onItemSelectedListener
    ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(SaveUser.this, android.R.layout.simple_spinner_item, categories2);
    dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    country.setAdapter(dataAdapter2);
    country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() 
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 

            String item = parent.getItemAtPosition(position).toString();
            Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
        

        @Override
        public void onNothingSelected(AdapterView<?> parent) 
            // can leave this empty
        

    );



【讨论】:

以上是关于Android:两个 Spinner onItemSelected()的主要内容,如果未能解决你的问题,请参考以下文章

Android:两个 Spinner onItemSelected()

为啥 Android Spinner onItemSelectedListener 有两个相同的参数

Android之Spinner使用详解

Android 下拉列表Spinner

Android开发系列(二十一):Spinner的功能和使用方法以及实现列表选择框

ActionBar 宽度中的两个 Spinner 菜单项