Android Spinner OnItemSelected不起作用

时间:2013-07-26 14:08:59

标签: java android

我有三个不同的Spinner对象(其中两个有自定义布局,一个 - spMode - 是标准的)。

按下OkayButton时,Toast应显示3个数字:SpinnerItemNumber of Mode,Color1和Color2。但每按一次按钮,模式为0。其他人工作正常。 Just Mode显示即使我在App中选择了第2或第3项,也会选择第0项。

这是相关代码;任何想法为什么?

public class NewTimer extends Activity implements OnItemSelectedListener,OnClickListener
    {
    Button btnOkay;
    EditText etTime;
    int mode, minutes, color1, color2;
    String[] strings = {"","",
        "", "", "", ""};

    int arr_images[] = { R.drawable.yellow,
        R.drawable.orange, R.drawable.red, 
        R.drawable.blue, R.drawable.green, R.drawable.white};

    public void onClick(View v)
    {
        if (v == btnOkay)
        {
            Toast.makeText(v.getContext(), Integer.toString(mode) + Integer.toString(color1) + Integer.toString(color2), Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.newtimer);
        Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
        spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
        spColor1.setOnItemSelectedListener(this);
        Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
        spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
        spColor2.setOnItemSelectedListener(this);
        etTime = (EditText) findViewById(R.id.etTime);
        btnOkay = (Button) findViewById(R.id.btnOkay);
        btnOkay.setOnClickListener(this);

        Spinner spMode = (Spinner) findViewById(R.id.spMode);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
                                                                             R.array.spMode_items, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spMode.setAdapter(adapter);
    }

    public void onItemSelected(AdapterView<?> parent,
                               View v, int pos, long id)
    {
        switch (parent.getId())
        {
            case R.id.spMode:
                mode = pos;
                break;

            case R.id.spColor1:
                color1 = pos;
                break;

            case R.id.spColor2:
                color2 = pos;
                break;
        }
    }

    public void onNothingSelected(AdapterView<?> parent)
    {

    }



    public class MyAdapter extends ArrayAdapter
    {
        public MyAdapter(Context context, int textViewResourceId,
                         String[] objects)
        {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent)
        {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            return getCustomView(position, convertView, parent);
        }
        public View getCustomView(int position, View convertView, ViewGroup parent)
        {
            LayoutInflater inflater=getLayoutInflater();View row=inflater.inflate(R.layout.row, parent, false);
            TextView label=(TextView)row.findViewById(R.id.color);
            label.setText(strings[position]);
            ImageView icon=(ImageView)row.findViewById(R.id.image);
            icon.setImageResource(arr_images[position]);
            return row;
        }
    }
    }

1 个答案:

答案 0 :(得分:1)

您永远不会为该微调器设置setOnItemSelectedListener

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newtimer);
    Spinner spColor1 = (Spinner)findViewById(R.id.spColor1);
    spColor1.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
    spColor1.setOnItemSelectedListener(this);
    Spinner spColor2 = (Spinner)findViewById(R.id.spColor2);
    spColor2.setAdapter(new MyAdapter(NewTimer.this, R.layout.row, strings));
    spColor2.setOnItemSelectedListener(this);
    etTime = (EditText) findViewById(R.id.etTime);
    btnOkay = (Button) findViewById(R.id.btnOkay);
    btnOkay.setOnClickListener(this);

    Spinner spMode = (Spinner) findViewById(R.id.spMode);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
                                                                         R.array.spMode_items, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spMode.setAdapter(adapter);
    spMode.setOnItemSelectedListener(this); }

public void onItemSelected(AdapterView<?> parent,
                           View v, int pos, long id) {
    switch (parent.getId())
    {
        case R.id.spMode:
            mode = pos;
            break;

        case R.id.spColor1:
            color1 = pos;
            break;

        case R.id.spColor2:
            color2 = pos;
            break;
    } }