Popular Spinner with Sqlite Database -

Asked

Viewed 3,051 times

2

I have the following table in the database:

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

I need to fill out a SPINNER with this data, so that the class NAME is visualized but in fact when the user chooses the class name is assisted the class _id.

  • In this link: https://answall.com/questions/342927/popular-spinner-comdados-do-sqlite

2 answers

5

Create a class to represent a gang:

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 from 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 by the method getReadableDatabase() of its inherited class of Sqliteopenhelper

Use that Arraylist in the construction of Adapter:

ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, turmasArray);

The Adapter will use the method toString() class Gang to obtain the string that will appear on Spinner.

You can get the selected class on Spinner in this way:

Turma turmaSelecionada = ((Turma)spinner.getSelectedItem());
  • How would you play from Cursor to an Arrayadapter of the Class type?

  • See the Reply Edition.

3

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, let’s create a Adapter customized to popular the Spinner :

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 the OnItemSelectedListener :

     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);

Browser other questions tagged

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