Insert selected SPINNER item into SQLITE

Asked

Viewed 934 times

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.

  • So @Paulo Rodrigues, after loading the spinner, I have an instantiated boot with setOnClicllistener to do the Insert in sqlite. myDb = new DatabaseHelper(this);&#xA; public void AddData() {&#xA; salvar.setOnClickListener(new View.OnClickListener() {&#xA; @Overridepublic void onClick(View v) {&#xA; boolean isInserted = myDb.insertData

2 answers

1

You failed to copy the method you had to implement, onItemSelected.

You must implement the following in this method:

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        //Ele irá retornar o objeto selecionado no Spinner conforme o tipo que você utilizou para preencher o ArrayAdapter, no seu caso.
        objeto = parent.getItemAtPosition(position);
        // Após pegar o objeto selecionado é só fazer a inserção na base de dados
        myDb.insertData(objeto);
    }

I hope I helped! Good luck!

  • I implemented the method, and it was possible to do the Insert in sqlite, but it saved the following data in sqlite : android.widget.Spinner{3088dda1 VFED. .C. ........ 245,270-688,343 #7f080050 app:id/sp_brand} public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {&#xA;&#xA;String objeto = parent.getItemAtPosition(position).toString()&#xA;&#xA;myDb.insertData(objeto.toString());

1


Once you have the list of items from Spinner with this code snippet:

List<Integer> constante = db.GetAllConstante();

Just use it to get which item is selected:

constante.get(spinner.getSelectedItemPosition())

And then, this value be entered into the database, along with its other values. This discards the use of the OnItemSelectedListener, since its intention is not to carry out this action by selecting an item from Spinner yes on the button Save (unless you are using for another action).

  • I implemented the above code, and Insert worked, but it entered all the spinner data even though I used getSelectedItemPosition. List<String> mod = myDb.Getallmodels(); mod.get(spiner_templates.getSelectedItemPosition()); myDb.insertData(mod.toString());

  • Is not mod.toString() that you should insert, since mod and its list whole. You have to insert the mod.get(spiner_modelos.getSelectedItemPosition()).

  • It worked, String rec= mod.get(spiner_modedlos.getSelectedItemPosition(); myDb.insertData(rec.toString()); Vlw !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.