'Retrieve and display corresponding data from firebase when clicking on spinner item in Android Studio Java

I'm making an application that pulls all the information about a lake when selected. I have a lake database in firebase with all the important information like name, location, hours, fish found, etc. I have populated a spinner with the names of all the lakes. Now I want to click on a specific lake name from the spinner to get all of the information displayed below the spinner, I have done a ton of research but nothing helps.

This is the code I have so far

public class places_to_fish extends AppCompatActivity {

Spinner spinner;
DatabaseReference databaseReference;
List<String> names;
String selectedName;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_places_to_fish);
    names = new ArrayList<>();


    FirebaseDatabase database = FirebaseDatabase.getInstance();
    databaseReference = FirebaseDatabase.getInstance().getReference();
    databaseReference.child("Places").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot childSnap : snapshot.getChildren()) {

                String spinnerName = childSnap.child("Name").getValue(String.class);
                names.add(spinnerName);
            }
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(places_to_fish.this, android.R.layout.simple_spinner_item, names);
                arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
                spinner.setAdapter(arrayAdapter);

                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        selectedName = arrayAdapter.getItem(i);




                    }

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

                    }
                });
            }

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

        }


    });

    





}

}

Click to see Firebase Database



Solution 1:[1]

create class Lake.java

    public class Lake {
    public String Fish, Hours, Location, Name, Zipcode ;

    public Lake(){}
    public Lake(String Fish, String Hours,String Location, String Name, String Zipcode){
        this.Fish = Fish;
        this.Hours = Hours;
        this.Location = Location;
        this.Name = Name;
        this.Zipcode = Zipcode;
    }
}

then, in your current class declare a HashMap

HashMap<String, Lake> map = new HashMap<String, Lake>();

now, initialize the map and add all the lakes into it

map.clear();
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                Iterator <DataSnapshot> it = snapshot.getChildren().iterator();
                while(it.hasNext() ){
                    DataSnapshot item = it.next();
                    name = item.child("full_name").getValue().toString().trim();
                    String Fish = item.child("Fish").getValue().toString().trim();
                    String Hours = item.child("Hours").getValue().toString().trim();
                    String Location = item.child("Location").getValue().toString().trim();
                    String Name = item.child("Name").getValue().toString().trim();
                    String Zipcode = item.child("Zipcode").getValue().toString().trim();
                    Lake tmp_Lake = new Lake(Fish,Hours,Location,Name,Zipcode);
                    map.put(Name,tmp_Lake);
                }

            }

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

            }
        });

now you have a map with all the lakes info (sorted by their names) so now, when you choose lake, you can take all the details from the map. this code will create array "names" with the lake names

public void addToList(){
        String [] names = new String[map.size()];
        int i = 0;
        for(User tmp : map.values()){
            names[i] = tmp.Name;
            i++;
        }

call this method after you initialize the map and finally, displaying the selected lake info by creating a string and load it to a textbox

ArrayAdapter arrayAdapter  = new ArrayAdapter(place_to_fish.this, android.R.layout.simple_list_item_1,names);
    spinner.setAdapter(arrayAdapter);
    spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
            String lake_name = names[i];
            Lake tmp = map.get(names[i]);
            String [] Lake_details = new String[5];
            Lake_details[0] = "Name: "+tmp.Name + "\n";
            Lake_details[1] = "Fish: "+tmp.Fish + "\n";
            Lake_details[2] = "Location: "+tmp.Location + "\n";
            Lake_details[3] = "Hours: "+tmp.Hours + "\n";
            Lake_details[4] = "Zipcode "+tmp.Zipcode + "\n";
            String text = "";
            for (int i = 1; i < Lake_details.length; i++)
                text += Lake_details[i];
            full_text.setText(text);//"full_text" is a clear text box
        }
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1