Spinner does not display the selected value

0

I retrieve the data from Firebase and when I select one in the spinner, it does not identify the selected.

This is the fragment

spinner = (Spinner) view.findViewById(R.id.spn_l_estados);

try {
 arrayAdapter = new ArrayAdapter(
  getContext().getApplicationContext(),
  R.layout.spinner_item,
  EstadosWebFb.retornaEstados()
) {
};
} catch (Exception e) {
 e.printStackTrace();
}

arrayAdapter.setDropDownViewResource(R.layout.spinner_dropdown_item);

spinner.setAdapter(arrayAdapter);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, 
long id) {
   Toast.makeText(getContext().getApplicationContext(),
    "POSICAO" + position,
    Toast.LENGTH_LONG).show();

arrayAdapter.notifyDataSetChanged();

}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

This is the class to return the data:

public static List retornaEstados() {
    FirebaseDatabase firebaseDatabase;
    final DatabaseReference databaseReference;

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference();

    final List list = new ArrayList();

    databaseReference.child("Estados").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (int i = 0; i < dataSnapshot.getChildrenCount(); i++) {
                list.add(i, dataSnapshot.child(String.valueOf(i)).getValue());
            }
            Log.i("DATASNAP", list.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return list;
}

Edit 1:

I made an ENUM to get the data and set in the ArrayAdapter and the code worked:

public enum Estados {

AC("Acre/AC"),
AL("Alagoas/AL"),
AP("Amapá/AP"),
AM("Amazonas/AM"),
BA("Bahia/BA"),
CE("Ceará/CE"),
DF("Distrito Federal/DF"),
ES("Espirito Santo/ES"),
GO("Goiás/GO"),
MA("Maranhão/MA"),
MT("Mato Grosso/MT"),
MS("Mato Grosso do Sul/MS"),
MG("Minas Gerais/MG"),
PA("Pará/PA"),
PB("Paraíba/PB"),
PR("Paraná/PR"),
PE("Pernambuco/PE"),
PI("Piauí/PI"),
RJ("Rio de Janeiro/RJ"),
RN("Rio Grande do Norte/RN"),
RS("Rio Grande do Sul/RS"),
RO("Rondônia/RO"),
RR("Roraima/RR"),
SC("Santa Catarina/SC"),
SP("São Paulo/SP"),
SE("Sergipe/SE"),
TO("Tocantins/TO");

String descricao;

Estados(String descricao){
    this.descricao = descricao;
}

@Override
public String toString() {
    return descricao;
}

//Itera sobre o proprio enum para retornar a lista de Estados
public static List<String> lista() {
    List list = new ArrayList();
    for (Estados estados : Estados.values()) {
        list.add(estados);
    }
    return list;
}

But I want to retrieve the values of the firebase, and the class I was using (the same one that does not have the value in the spinner) is as follows:

public class EstadosWebFb {
public static List retornaEstados() {
    FirebaseDatabase firebaseDatabase;
    final DatabaseReference databaseReference;

    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference();

    final List list = new ArrayList();

    databaseReference.child("Estados").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (int i = 0; i < dataSnapshot.getChildrenCount(); i++) {
                list.add(i, dataSnapshot.child(String.valueOf(i)).getValue());
            }
            Log.i("DATASNAP", list.toString());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    return list;
}
    
asked by anonymous 29.06.2017 / 16:23

1 answer

0

Use the getItemAtPosition method by passing the clicked parameter to get the value. See:

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView << ? > parent, View view, int position,
        long id) {

        String item = parent.getItemAtPosition(position);
        Toast.makeText(getContext().getApplicationContext(),
            "Item: " + item ,
            Toast.LENGTH_LONG).show();

        arrayAdapter.notifyDataSetChanged();

    }
    @Override
    public void onNothingSelected(AdapterView << ? > parent) {}
});
    
29.06.2017 / 16:37