Popular one Spinner with SQLite data

0

I have this class:

package com.example.userlocal.oficinadocabelo.Agenda;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Calendar;

import com.example.userlocal.oficinadocabelo.R;
import com.example.userlocal.oficinadocabelo.TelaInicial;
import com.example.userlocal.oficinadocabelo.banco.banco;
import com.example.userlocal.oficinadocabelo.funcionario.MostrarFuncionario;

public class AgendarF extends AppCompatActivity implements Button.OnClickListener {

    private SQLiteDatabase db;
    private EditText Nome,Dia,Corte,valor;
    private AlertDialog alerta;
    private ImageButton data;
    private Button btncadastrar,btnvoltar;
    static final int DATE_DIALOG_ID = 0;
    private String DataFinal;
    private Spinner Hora;
    private banco banco;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cadastrar_horario);
        data =(ImageButton) findViewById(R.id.imgdata);
        data.setOnClickListener(this);


        btncadastrar =(Button) findViewById(R.id.Btnsalvar);
        btnvoltar=(Button) findViewById(R.id.btnCancelar);
        Nome =(EditText) findViewById(R.id.edtNome);
        Hora =(Spinner) findViewById(R.id.spinerhorario);
        Dia = (EditText) findViewById(R.id.txtData);
        Corte =(EditText) findViewById(R.id.edtCorte);
        data=(ImageButton) findViewById(R.id.imgdata);
        valor=(EditText) findViewById(R.id.edtValor);






        ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.spinnerhorario, android.R.layout.simple_spinner_item);
        Hora.setAdapter(adapter);

        btncadastrar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String spvalor = Hora.getSelectedItem().toString();

                 if(Dia.getText().length() == 0 ||Hora.equals(null) ||Corte.getText().length() == 0){
                    Toast.makeText(getApplicationContext(), "Nenhum campo pode estar vazio", Toast.LENGTH_LONG).show();
                }

                else {

                        ControlarAgedamento ca = new ControlarAgedamento(getBaseContext());
                        String nomeString = Nome.getText().toString();
                        String diaString = Dia.getText().toString();
                        String horaString = spvalor;
                        String corteString = Corte.getText().toString();
                        String valorString = valor.getText().toString();

                        String resultado;

                        resultado = ca.insereAgenda(nomeString, diaString, horaString, corteString, valorString);
                     if(resultado.equals("Registro Inserido com sucesso")){
                         Intent i = new Intent(AgendarF.this, Agenda.class);
                         startActivity(i);
                         finish();
                         Toast.makeText(getApplicationContext(), "Horario Cadastrado", Toast.LENGTH_LONG).show();


                             }
                            else{
                         Toast.makeText(getApplicationContext(), "Horario Ocupado, Tente Outro Horário", Toast.LENGTH_LONG).show();

                     }


                }
            }
        });

    }
    @Override
    protected Dialog onCreateDialog(int id) {
        Calendar calendario = Calendar.getInstance();

        int ano = calendario.get(Calendar.YEAR);
        int mes = calendario.get(Calendar.MONTH);
        int dia = calendario.get(Calendar.DAY_OF_MONTH);

        switch (id) {
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this, mDateSetListener, ano, mes,
                        dia);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            String data = String.valueOf(dayOfMonth) + " /"
                    + String.valueOf(monthOfYear+1) + " /" + String.valueOf(year);
            Toast.makeText(AgendarF.this,
                    "DATA = " + data, Toast.LENGTH_SHORT)
                    .show();
            DataFinal= data;
            Dia.setText(DataFinal);
        }
    };

    @Override
    public void onClick(View v) {
        if (v == data)
            showDialog(DATE_DIALOG_ID);
    }
    //////////////Acaba Calendario//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






    }

I would like to share my Spinner with data from this bank here (only the names):

String sql = "CREATE TABLE "+TABELACLI+"("
                +IDCLI+ " integer primary key autoincrement,"
                +IMGCLI+ " blob,"
                +NOMECLI+ " text not null,"
                +TELEFONECLI+ " text not null,"
                +CELULARCLI+ " text not null,"
                +EMAILCLI+ " text not null,"
                +SEXOCLI+ " text not null,"
                +CODCLI+ " text not null"
                +");";
    
asked by anonymous 08.12.2016 / 02:08

1 answer

1

First you have to save the data in a list

// Criei um array de string, mas no caso pode usar um objeto sem problemas
String[] nome=new String[]{"pedro","joao"};

Then you just create an ArrayAdapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

adapter.setAdapter(adapter);
    
08.12.2016 / 19:31