从我的微调器获取信息时遇到一些问题,这就是我在我的活动中设置它的方式
color = (Spinner)findViewById(R.id.ledColor);
vibrate = (Spinner)findViewById(R.id.vibPattern);
populateSpinners();
color.setOnItemSelectedListener(new colorSelected());
vibrate.setOnItemSelectedListener(new vibrateSelected());
public void populateSpinners(){
ArrayAdapter<CharSequence> cAdapter;
cAdapter = ArrayAdapter.createFromResource(this, R.array.colors,android.R.layout.simple_spinner_item);
int cSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
cAdapter.setDropDownViewResource(cSpinnerDD);
color.setAdapter(cAdapter);
ArrayAdapter<CharSequence> vAdapter;
vAdapter = ArrayAdapter.createFromResource(this, R.array.vibrate, android.R.layout.simple_spinner_item);
int vSpinnerDD = android.R.layout.simple_spinner_dropdown_item;
vAdapter.setDropDownViewResource(vSpinnerDD);
vibrate.setAdapter(vAdapter);
}
public class colorSelected implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nColor = arg0.toString();
Log.v("EditContact",nColor);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
/********************************************************************************************/
public class vibrateSelected implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
arg0.getItemAtPosition(arg2);
nVibrate = arg0.toString();
Log.v("EditContact",nVibrate);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
但它没有得到微调器的选择我做错了什么?
答案 0 :(得分:1)
创建一个这样的公共方法:
public String returnString(AdapterView<?> parent,int position,long ID){
return parent.getSelectedItem().toString();
}
并将其放在onItemSelected方法中:
String yourtext = returnString(arg0, arg2, arg3);
答案 1 :(得分:0)
使用以下代码获取微调器选定值..
String value=spinner.getSelectedItem().toString();