Spinner does not display the selected value

Asked

Viewed 300 times

1

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

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 data return class:

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 take the data and set in 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 firebase values, and the class I was using (the same that doesn’t show 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;
}

1 answer

0

Use the method getItemAtPosition passing as parameter the position clicked to catch 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) {}
});
  • Using the Enum that includes both codes (mine and yours) worked. However I want to recover data from Firebase (class included in Edit), but none of the codes worked.

Browser other questions tagged

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