Android:两个 Spinner onItemSelected()

发布于 2025-01-05 06:07:29 字数 1372 浏览 2 评论 0原文

我在一个 Activity 中有两个旋转器(day_spin 和 time_spin)。我想将选定的 day_spin 值保存到变量中。是否可以区分同一个监听器中的两个旋转器?或者我是否必须为每个微调器编写自己的 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();
}

我希望你能帮助我!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

风透绣罗衣 2025-01-12 06:07:29
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) {
    if(parent.getId() == R.id.spinner1)
    {
      //do this                    
    }
    else if(parent.getId() == R.id.spinner2)
    {
      //do this
    }
}
故事与诗 2025-01-12 06:07:29
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;              
    }
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;              
    }
音盲 2025-01-12 06:07:29

onItemSelected 方法中

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

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

in onItemSelected method

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

String strName1 = yourFirstarray[i];
String strName2 = yourSecondarray[j];
烟酉 2025-01-12 06:07:29

试试这个...

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

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
}
}
三寸金莲 2025-01-12 06:07:29

同一类中有两个 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
        }

    });


}

Two Spinner in same class

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
        }

    });


}
心安伴我暖 2025-01-12 06:07:29
@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();
    }
}
@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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文