0

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

当按下 OkayButton 时,应该显示一个带有 3 个数字的 Toast:Mode、Color1 和 Color2 的 SpinnerItemNumber。但是每次按下按钮时,Mode 都是 0。其他工作正常。即使我在应用程序中选择了第 2 项或第 3 项,Just Mode 也显示选择了第 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;
        }
    }
    }
4

1 回答 1

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;
    } }
于 2013-07-26T14:12:02.357 回答