Popular a Spinner with Sqlite data

Asked

Viewed 398 times

0

I have that 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 popular my Spinner with data from that database 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"
                +");";
  • Gabriel, be careful with formatting. When pasting code, select it and use the "{ }" button to format as code (or Ctrl-K). Don’t use high boxes to write big sentences, it’s bad to read. But above all, show what you’ve done or know how to do. Do you know how to put something in a Spinner? Do you know how to read something from a Sqlite bank? You need to isolate the problem and bring only the part that really is your doubt.

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

Browser other questions tagged

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