Popular Spinner with SQLite Database -

1

I have the following table in the database:

CREATE TABLE turma(_id integer primary key autoincrement, nome varchar(20))

I need to fill in a SPINNER with this data, so that the class NAME is displayed, but actually when the user chooses the class name, the _id of the class is attached.

    
asked by anonymous 26.07.2016 / 16:40

2 answers

3

Create a class to represent a class :

public class Turma {

    private String nome;
    private int id;

    public Turma(int id, String nome){
        this.id = id;
        this.nome = nome;
    }

    public int getId(){
        return id;
    }

    public String getNome(){
        return nome;
    }

    @Override
    public String toString()
    {
        return nome;
    }
}

The method that gets the classes of the bank should return a ArrayList<Turma> :

public ArrayList<Turma> getTurmas(){
    ArrayList<Turma> turmas = new ArrayList<Turma>();
    Cursor cursor = db.query("turma", null, null, null, null, null, null);
    if(cursor != null && cursor.moveToFirst()){
        do{
            int id = cursor.getInt(0);
            String nome = cursor.getString(1);
            Turma turma = new Turma(id, nome);
            turmas.add(turma);
        }while(cursor.moveToNext());
    }
    return turmas;
}

Where db is an instance of SQLiteDatabase obtained through the getReadableDatabase() method of its inherited class from SQLiteOpenHelper

Use this ArrayList to construct the adapter :

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, turmasArray);
The adapter will use the toString() method of the Class class to get the string that will appear in Spinner .

You can get the selected class in Spinner this way:

Turma turmaSelecionada = ((Turma)spinner.getSelectedItem());
    
26.07.2016 / 17:07
2

I believe you have encapsulated this information in an Object:

Example:

class Turma {
    private Integer id;
    private String nome;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
}

Having this, we will create a% custom% to popular Adapter :

class TurmaAdapter extends ArrayAdapter<Turma>{

    public TurmaAdapter(Context context) {
        super(context, R.layout.spinner_adapter);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(null == convertView){
            convertView = getLayoutInflater().inflate(R.layout.spinner_adapter, parent, false);
        }
        //Pegamos a turma...
        final Turma item = getItem(position);
        // Adicionamos a turma a Tag da View...
        convertView.setTag(item);
        return convertView;
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getDropDownView(position, convertView, parent);
    }
}

Now let's set up Spinner :

     AdapterView.OnItemSelectedListener itemSelectedListener  =new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 

                // Vamos pegar o a Turma 
                final Turma turma =  Turma.class.cast(view.getTag());
               // Aqui voce poderá associar a turma! 
            }

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

            }
        };

 meuSpinner.setOnItemSelectedListener(itemSelectedListener);
    
26.07.2016 / 17:06