1

I'm new to Android and I'm trying to build a simple app that passes the value selected on a spinner to the database. But I can't figure out how to pass this value to the variable. I've read all of the threads related to the subject, but I just can't find the answer. Here is my code:

package com.example.marcelo.aulaandroid;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.marcelo.aulaandroid.Data.ClienteDataSource;
import com.example.marcelo.aulaandroid.Modelo.Cliente;

public class CadastroClientesActivity extends AppCompatActivity {

    boolean estadoCadastro = true;
    ClienteDataSource ds;
    private Cliente cliente = new Cliente();
    private String spnGenero;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cadastro_clientes);

    final Spinner spinner = (Spinner) findViewById(R.id.spinGenero);

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.genero_array, android.R.layout.simple_list_item_1);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String item = parent.getItemAtPosition(position).toString();
            //Toast.makeText(parent.getContext(), "Selected..." + item, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    this.startDataBase();
    spnGenero = (String) findViewById(R.id.spinGenero);

    Intent intent = getIntent();

    if(intent != null){
        if(intent.getSerializableExtra("objCliente") != null){
            estadoCadastro = false;
            cliente = (Cliente)intent.getSerializableExtra("objCliente");
            spnGenero.setText(cliente.getGenero());
        }
    }

}


public void salvarCliente(View view){
    cliente.setGenero(spnGenero.getText().toString());

    if(estadoCadastro){
        ds.inserir(cliente);
    }
    else{
        ds.atualizar(cliente);
    }

    finish();
}

private void startDataBase(){
    ds = new ClienteDataSource(this);
    ds.open();
}

}

When I debug this code, I can see that the toast is returning the value of the variable "item" correctly, but I need it to be assigned to "spnGenero" and, afterwards, to another variable on a class bean outside of this class. How can I do that?

  • Why don't you just set it inside you `onItemSelected` event? – DCruz22 Apr 21 '16 at 19:59
  • Like, for example, Spinner spnGenero = parent.getItemAtPosition(position).toString(); ? Sorry, I'm new to Java... This is returning a string. If I change to String spnGenero = parent.getItemAtPosition(position).toString(); I get an error everytime I call the spnGenero variable saying "Cannot resolve symbol 'spnGenero'" – Marcelo Xavier Vieira Apr 21 '16 at 22:50
  • Are you declaring the `spnGenero` outside the method? – DCruz22 Apr 22 '16 at 12:12

1 Answers1

0

I've found a topic here saying I can answer my own question. I finally have a solution for the problem, thanks the third answer to the topic how to use spinner, the ideas provided by user DCruz22 and a little bit of imagination.

First, I'm modifying the declaration of the variable spnGenero. Instead of private Spinner spnGenero; I'm using private String spnGenero;

Now, inside the onItemSelected event, I'm assigning the spinner value (a string) to the spnGenero variable:

spnGenero = parent.getItemAtPosition(position).toString();

After I call this.startDataBase(); I discover I don't need the line that says: spnGenero = (Spinner) findViewById(R.id.spinGenero);

The same applies to the code that says: spin_val.setText(cliente.getGenero());

The most important modification is inside the salvarCliente()callback. It needs to get the spnGenero's value (which is now officially a String) and pass it to my bean. I've changed it to:

cliente.setGenero(spnGenero);

And, of course, setGenero is a method declared inside my bean.

I hope this answer will help others that are facing a similar problem.

Community
  • 1
  • 1