简体   繁体   English

Android:两个 Spinner onItemSelected()

[英]Android: two Spinner onItemSelected()

I have two spinners (day_spin and time_spin) in one Activity .我在一个Activity有两个微调器(day_spin 和 time_spin)。 I want to save the selected day_spin value into a variable.我想将选定的 day_spin 值保存到一个变量中。 Is it possible to differenciate between the two spinners in the same Listener ?是否可以区分同一个Listener的两个微调Listener Or do I have to write my own Listener class for each spinner?还是我必须为每个微调器编写自己的Listener类?

Also I want to get the number of the selected item.另外我想获取所选项目的编号。 For example I have String Array with 6 elements.例如,我有 6 个元素的字符串数组。 I select number 3. How can I get the id?我选择数字 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);

This is my Listener :这是我的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!我希望你能帮帮我!

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
    }
}
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;              
    }

in onItemSelected methodonItemSelected方法中

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

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

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
}
}
@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();
    }
}

Two Spinner in same class同一班级的两个 Spinner

I try a lot of this but at the last, I got a working Code.我尝试了很多,但最后,我得到了一个有效的代码。

    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
        }

    });


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM