我不能让微调器写入数据库。我阅读了关于微调器的文档,但我确实做错了什么。有人可以提一些建议吗? 我已经阅读了很多教程,但是我感到很困惑。如何让我在输入时让微调器写入onClick中的数据库,想要再加1个包括微调器。
ContextMenu
答案 0 :(得分:0)
您需要保留Spinner
中的选定值,然后在您用于插入数据库的方法中使用它
另外,您应该在AddData();
btn_save
活动中致电onClick
!
public class AddActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
DatabaseHelper mylogsDB;
Button btn_save;
EditText etHours;
Spinner etShift;
private String spinnerSelectedItem;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
etHours = (EditText) findViewById(R.id.editText);
etShift = (Spinner) findViewById(R.id.spinner);
btn_save = (Button) findViewById(R.id.btn_save);
mylogsDB = new DatabaseHelper(this);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddData();
}
});
// Spinner click listener
etShift.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<>();
categories.add("Πρωί");
categories.add("Μεσημέρι");
categories.add("Νύχτα");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
etShift.setAdapter(dataAdapter);
}
public void onItemSelected (AdapterView <?> parent, View view,int position, long id) {
// On selecting a spinner item
spinnerSelectedItem = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Έχετε επιλέξει: " + spinnerSelectedItem, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
public void AddData() {
String hours = etHours.getText().toString();
//spinnerSelectedItem should have a value
if(!hours.equals("") && !spinnerSelectedItem.equals("")){
boolean insertData = mylogsDB.addData(hours, spinnerSelectedItem);
if (insertData == true) {
Toast.makeText(AddActivity.this, "Η αποθήκευση ολοκληρώθηκε επιτυχώς.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(AddActivity.this, "Ώχ,Κάτι πήγε στραβά.", Toast.LENGTH_LONG).show();
}
}else{
//do something
}
}
}