0
How to identify the selected item in the SPINNER and insert it into the SQLITE database.
I can do the Insert in Sqlite using the data provided in Edittext, however I am not able to create a way to do something similar with SPINNER.
Code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recadastramento);
marca = (EditText) findViewById(R.id.ed_marca);
modelo = (EditText) findViewById(R.id.ed_modelo);
constante = (EditText) findViewById(R.id.ed_constante);
data_fabricacao = (EditText) findViewById(R.id.ed_fabricacao);
salvar = (Button) findViewById(R.id.btn_salvar);
myDb = new DatabaseHelper(this);
AddData();
spiner_constante = (Spinner) findViewById(R.id.sp_constante);
spiner_constante.setOnItemSelectedListener(this);
loadSpinnerData();
private void loadSpinnerData() {
DatabaseHelper db = new DatabaseHelper(getApplicationContext());
List<Integer> constante = db.GetAllConstante();
ArrayAdapter<Integer> dataAdapter3 = new ArrayAdapter<Integer> (this,android.R.layout.simple_spinner_item,constante);
spiner_constante.setAdapter(dataAdapter3);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
public void AddData() { // AQUI É FEITO O INSERT DOS DADOS DOS EDITTEXT, NO SQLITE
salvar.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDb.insertData(marca.getText().toString(), modelo.getText().toString(), constante.getText().toString(), data_fabricacao.getText().toString());
}
}
To get the selected item from Spinner just use
spinner.getSelectedItemPosition()
. I did not understand very well your code to indicate where to put this excerpt. If you can explain better.– Paulo Rodrigues
So @Paulo Rodrigues, after loading the spinner, I have an instantiated boot with setOnClicllistener to do the Insert in sqlite.
myDb = new DatabaseHelper(this);
 public void AddData() {
 salvar.setOnClickListener(new View.OnClickListener() {
 @Overridepublic void onClick(View v) {
 boolean isInserted = myDb.insertData
– Danilo Freitas