微调的setOnItemSelectedListener不叫

微调的setOnItemSelectedListener不叫

本文介绍了微调的setOnItemSelectedListener不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的 setOnItemSelectedListener 以指定哪个项目的微调选择,但我认为它不叫。它应打印它的工作原理的程序运行时。

XML code:

 <微调
    机器人:ID =@ + ID /数量
    机器人:layout_width =94dp
    机器人:layout_height =27dp
    机器人:layout_x =11DP
    机器人:layout_y =118dp
     />

Java的code:

 公共类数量扩展活动
{微调量;
    公共无效的onCreate(捆绑savedInstanceState)
    {
    量=(微调)findViewById(R.id.quantity);    quantity.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener()
            {
                公共无效onItemSelected(适配器视图<>母公司,观景,INT POS,长I​​D)
                {
                    对象项目= parent.getItemAtPosition(POS)                    的System.out.println(它的工作......);                }                公共无效onNothingSelected(适配器视图<>母公司)
                {                }
            });
    }}


解决方案

试试这个

  @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.Your_Layout);            / *为填补你的微调* /
        清单<串GT; SpinnerArray =新的ArrayList<串GT;();
        SpinnerArray.add(项目1);
        SpinnerArray.add(项目2);
        SpinnerArray.add(项目3);
        SpinnerArray.add(项目4);
        SpinnerArray.add(项目5);        ArrayAdapter<串GT;适配器=新ArrayAdapter<串GT;(这一点,
                android.R.layout.simple_spinner_item,SpinnerArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        微调微调=(微调)findViewById(R.id.quantity);
        spinner.setAdapter(适配器);        spinner.setOnItemSelectedListener(新OnItemSelectedListener(){            @覆盖
            公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
                    INT ARG2,长ARG3){
                // TODO自动生成方法存根
                对象项目= arg0.getItemAtPosition(ARG2);
                如果(项目!= NULL){
                    Toast.makeText(MainActivity.this,item.toString(),
                            Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(MainActivity.this,入选
                        Toast.LENGTH_SHORT).show();            }            @覆盖
            公共无效onNothingSelected(适配器视图<>为arg0){
                // TODO自动生成方法存根            }
        });
    }

I used setOnItemSelectedListener to specify which item is selected in a spinner, but i think it is not called. it should print "It works" when the program runs.

XML code:

<Spinner
    android:id="@+id/quantity"
    android:layout_width="94dp"
    android:layout_height="27dp"
    android:layout_x="11dp"
    android:layout_y="118dp"
     />

Java code:

public class quantity extends Activity
{

Spinner  quantity;


    public void onCreate(Bundle savedInstanceState)
    {
    quantity=(Spinner)findViewById(R.id.quantity);

    quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
            {
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
                {
                    Object item = parent.getItemAtPosition(pos);

                    System.out.println("it works...   ");

                }

                public void onNothingSelected(AdapterView<?> parent)
                {

                }
            });


    }

}
解决方案

Try this

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.Your_Layout);

            /*for fill your Spinner*/
        List<String> SpinnerArray = new ArrayList<String>();
        SpinnerArray.add("Item 1");
        SpinnerArray.add("Item 2");
        SpinnerArray.add("Item 3");
        SpinnerArray.add("Item 4");
        SpinnerArray.add("Item 5");

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, SpinnerArray);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner spinner = (Spinner) findViewById(R.id.quantity);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                Object item = arg0.getItemAtPosition(arg2);
                if (item!=null) {
                    Toast.makeText(MainActivity.this, item.toString(),
                            Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(MainActivity.this, "Selected",
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub

            }
        });
    }

这篇关于微调的setOnItemSelectedListener不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:56