Popular Spinner with firebase data

0

This is the code for the "Activity Room"

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 a "Scheduling activity" where I want to put this list of previously created rooms in the spinner.

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 equal in the scheduling activity? Could you exemplify it for me?

    
asked by anonymous 30.08.2017 / 10:56

1 answer

0

You can create another one or save this list in memory.

Creating another: Just as you did to load the sample course list, however, you will set the spinner, spinner.setAdapter(adapter) .

Pro:

  • online data
  • No intricate memory list control.

"Recovering" list: In fact you will have to pass the list to this Activity in some way, such as Intent In the previous activty to the Schedule would do:

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

In ScheduleActivity you 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);

Note: Sala should implement Serializable

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

I often create another and be happy, but each case is a case.

    
30.08.2017 / 14:01