Spinner value from Firebase, On new data add spinner Item Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Spinner value from Firebase, On new data add spinner Item without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have done my project using Spinner and Firebase realtime database read. On newly add value, spinner will not remove the old value, how to do it?

Here the preview https://photos.app.goo.gl/jYTyJvcDpqBeGDTg9

“POS” is the new item I add on my database, here the preview https://photos.app.goo.gl/mH14qQ54ra3ULBZH8

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

    spinner = (Spinner) findViewById(R.id.tujuan_spinner);

    databaseReference = FirebaseDatabase.getInstance().getReference("Spinner_Tujuan");

    spinnerDataList = new ArrayList<>();
    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerDataList);

    spinner.setAdapter(adapter);
    retrieveData();

When I add new Value, spinner still keep old value which its become dupilicate value. How can I remove old value when new Value are inserted?

    //HERE
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, final int i, long l) {
            spinner.setSelection(i);
            final String value =spinner.getSelectedItem().toString();

            if (value.equals("SILA PILIH")){
                //DO nothing

            }if (value.equals("Pegawai")){
                bukasenaraiPegawai();
            } else {
                admin.setValue(value);
                openDialogInfo();
                spinner.setSelection(0);
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

public void retrieveData(){
    listener = databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot item : dataSnapshot.getChildren()){
                spinnerDataList.add(item.getValue().toString());
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}
}

Answer

Every time your onDataChange method is called, the dataSnapshot contains a snapshot of all data at databaseReference. So you’ll need to clear the existing data from the spinnerDataList to prevent duplicates:

listener = databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        spinnerDataList.clear();
        for (DataSnapshot item : dataSnapshot.getChildren()){
            spinnerDataList.add(item.getValue().toString());
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});
We are here to answer your question about Spinner value from Firebase, On new data add spinner Item - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji