我想在按下保存按钮时将所选的微调器项插入数据库。 有两个旋转器。一个是从数组填充而另一个是从sqlite d / b填充。 如何为两个微调器获取所选项的字符串。 我的代码如下:
公共类addLecture扩展AppCompatActivity实现AdapterView.OnItemSelectedListener {
Spinner spinner_day;
Spinner spinner_subject;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_lecture);
spinner_subject = (Spinner) findViewById(R.id.spinner_subject);
spinner_day = (Spinner) findViewById(R.id.spinner_day);
loadSpinner_subjectData();
loadSpinner_dayData();
}
private void loadSpinner_subjectData(){
SubjectDbHelper subjectDbHelper=new SubjectDbHelper(getApplicationContext());
List<String> spinner_name=subjectDbHelper.getSpinnerItem();
ArrayAdapter<String> spinner_adapter= new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,spinner_name);
spinner_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_subject.setAdapter(spinner_adapter);
}
private void loadSpinner_dayData(){
String day_name[]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
ArrayAdapter<String> spinner_dayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,day_name);
spinner_dayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_day.setAdapter(spinner_dayAdapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//not working String label=parent.getItemAtPosition(position).toString();
// not working String spinner_item_day=spinner_day.getItemAtPosition(position).toString();
/*not working SQLiteCursor subject=(SQLiteCursor)parent.getItemAtPosition(position);
String spinner_item_subject=String.valueOf(subject.getString(position));*/
//log is not shown Log.d("spinner",spinner_item_day);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
答案 0 :(得分:0)
在你的ItemSelectedListener中,你没有指定它正在侦听哪个微调器,所以你的代码应该是这样的:
spinner_day.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedDay = (String) parent.getItemAtPosition(position);
// or parent.getSelectedItem();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
同样适用于其他微调器,试试这个,如果这不起作用,请告诉我。