Popular Spinner with firebase data

Asked

Viewed 227 times

0

That’s the code for "Activity Sala"

public class CadastroCursoActivity extends AppCompatActivity {

    private EditText nomeCurso;
    private ListView lv_Curso;
    private Toolbar toolbarCurso;

    private List<Curso> listCurso = new ArrayList<Curso>();
    private ArrayAdapter<Curso> arrayAdapterCurso;

    private DatabaseReference databaseReference ;
    private FirebaseDatabase firebaseDatabase;

    private Curso cursoSelecionado;

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

        nomeCurso=(EditText)findViewById(R.id.et_NomeCurso);
        lv_Curso=(ListView)findViewById(R.id.lv_Curso);
        toolbarCurso=(Toolbar)findViewById(R.id.toolbar);
        toolbarCurso.setTitle("Curso");
        setSupportActionBar(toolbarCurso);
        inicializarFirebase();
        EventoDatabase();

        lv_Curso.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cursoSelecionado = (Curso)parent.getItemAtPosition(position);
                 nomeCurso.setText(cursoSelecionado.getNome());

            }
        });
    }
    private void inicializarFirebase(){
        FirebaseApp.initializeApp(CadastroCursoActivity.this);
        firebaseDatabase = FirebaseDatabase.getInstance();
        firebaseDatabase.setPersistenceEnabled(true);
        databaseReference =firebaseDatabase.getReference();
    }
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_cadastro_curso,menu);
        return super.onCreateOptionsMenu(menu);
    }

    private void EventoDatabase(){
        databaseReference.child("Curso").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                listCurso.clear();
                for (DataSnapshot objdataSnapshot:dataSnapshot.getChildren()){
                    Curso curso = objdataSnapshot.getValue(Curso.class);
                    listCurso.add(curso);
                }
                arrayAdapterCurso=new ArrayAdapter<Curso>(CadastroCursoActivity.this,
                        android.R.layout.simple_list_item_1,
                        listCurso);
                lv_Curso.setAdapter(arrayAdapterCurso);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }


    public boolean onOptionsItemSelected(MenuItem item) {
        try {

            int id = item.getItemId();
            if (id == R.id.menu_novo) {

                Curso curso = new Curso();
                curso.setId(UUID.randomUUID().toString());
                curso.setNome(nomeCurso.getText().toString());
                databaseReference.child("Curso").child(curso.getId()).setValue(curso);
                limparCampos();

            } else if (id == R.id.menu_atualizar) {
                Curso curso = new Curso();
                curso.setId(cursoSelecionado.getId());
                curso. setNome(nomeCurso.getText().toString());
                databaseReference.child("Curso").child(curso.getId()).setValue(curso);
                limparCampos();

            } else if (id == R.id.menu_deletar) {
                Curso curso = new Curso();
                curso.setId(cursoSelecionado.getId());
                databaseReference.child("Curso").child(curso.getId()).removeValue();
                limparCampos();
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }
    private void limparCampos(){
        nomeCurso.setText("");
    }

}

I created an "Activity Scheduling" where I want to put in the spinner this list of previously created rooms.

public class AgendamentoActivity extends AppCompatActivity {
    private Toolbar toolbarAgendamento;
    private Spinner spinnerSala;
    private Spinner spinnerCurso;
    private Sala sala;
    private ArrayAdapter<Sala> arrayAdapterSala;

    private DatabaseReference databaseReference ;
    private FirebaseDatabase firebaseDatabase;

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

        toolbarAgendamento=(Toolbar)findViewById(R.id.toolbar);
        toolbarAgendamento.setTitle("Agendamento");
        setSupportActionBar(toolbarAgendamento);

        spinnerSala=(Spinner)findViewById(R.id.spinnerSala);

        inicializarFirebase();
        //carregar dados
    }

    private void inicializarFirebase() {
        FirebaseApp.initializeApp(AgendamentoActivity.this);
        firebaseDatabase = FirebaseDatabase.getInstance();
        firebaseDatabase.setPersistenceEnabled(true);
        databaseReference =firebaseDatabase.getReference();
    }
}

How do I call this list? Or do I need to create another one like in Schedule Activity? Could you exemplify it for me?

1 answer

0

You can create another or store this list in memory.

Creating another one: Also like you did to load the example Bear list, but will set in spinner, spinner.setAdapter(adapter).

Pro:

  • online data
  • No list control complexity in memory.

"Retrieving" list of: In fact you will have to pass the list to that Activity in some way, such as for example Intent On the activty prior to Scheduling would:

Intent intent = new Intent(this, AgendamentoActivity.class);
intent.putExtra("listaSala", (Serializable) listaSala);
startActivity(intent);

In Scheduling Activity would:

List<Sala> listaSala = (List<Sala>) getIntent().getSerializableExtra("listaSala");
ArrayAdapter<Sala> arrayAdapterSala = new ArrayAdapter<Sala>(this,
                        android.R.layout.simple_list_item_1,
                        listaSala);
spinnerSala.setAdapter(arrayAdapterCurso);

Obs.: Sala must implement Serializable

Pro: dimunui a query that can often be unnecessary (or not)

I tend to create another and be happy, but every case is a case.

Browser other questions tagged

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