其实我在服务器上有一个创造微调器的价值。我试图为所有微调器设置setOnClickListener,但是它会出错。请在下面参考我的代码。


public static Spinner[] clickspin = new Spinner[nvalue];//nvalue = 2 (get from server)
for(int i=0;i<nvalue;i++)
{
clickspin[i]=new Spinner(this);
        clickspin[i].setLayoutParams(new TableRow.LayoutParams(90/*length of combo box*/, TableRow.LayoutParams.WRAP_CONTENT));
        clickspin[i].setAdapter(adapter1);
        clickspin[i].setId(i);
        clickspin[i].setClickable(true);
        clickspin[i].setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            {
                int n = v.getId();

            }
        });
}



  如何为每个微调器设置setOnClickListener

最佳答案

对于Spinners,我们使用OnItemSelectedListener

clickspin[i].OnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

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

    public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
    }
});


请参见Spinners guide

10-08 18:26