user10997009
user10997009

Reputation: 142

Click Spinner Item and fetch the spinner selected item data

In-app, I have a spinner, spinner has some option and this option store in firebase, the option are showing fine but when I click the spinner item it's showing some string in textview from the database. but it's show nothing. Suppose I have two data, first is name and second is age and I show the name in spinner item the when I clicked spinner it shows the age in textview

list = new ArrayList<>();
    retriveData();
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
            String selected = list.get(position).toString();
            Log.e("Clicked:",""+selected);
            reference.child("result").child(selected).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for (DataSnapshot ds : snapshot.getChildren()) {

                        String fAge = ds.child("age").getValue(String.class);
                        
                        tv_1st.setText(fAge);
                    }
                }

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

                }
            });
        }

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

        }

    });

}

public void retriveData() {
    reference.child("result").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            list.clear();
            for (DataSnapshot item : snapshot.getChildren()) {
                list.add(item.child("name").getValue().toString());
            }
            adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_style, list);
            Collections.reverse(list);
            spinner.setAdapter(adapter);
        }

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

        }
    });
}

my database structure

enter image description here

How can i show this data in textview?

Upvotes: 0

Views: 731

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599601

When you read the data from your database for the adapter, you should keep all data that you need. This includes the name, age, but also the key, which you'll need if you ever want to update/delete the data. This also saves you from having to do the additional loading of data when the user selects an item in the spinner, as you'll already have all data in the app.


The best way to do this is to keep an array of DataSnapshot objects, instead of just an array of names. Then in your layout for the spinner, you just get the name property from each DataSnapshot as you now do in your onDataChange.


A less pretty way, but easier to show based on your current code, is to keep the names, ages, and keys in a bunch of arrays:

nameList = new ArrayList<>();
ageList = new ArrayList<>();
keyList = new ArrayList<>();

And then populate all of these when loading the data:

reference.child("result").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        list.clear();
        for (DataSnapshot item : snapshot.getChildren()) {
            nameList.add(item.child("name").getValue(String.class));
            ageList.add(item.child("age").getValue(Long.class));
            keyList.add(item.getKey());
        }
        adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_style, nameList);
        Collections.reverse(nameList);
        spinner.setAdapter(adapter);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // never ignore erros
    }
});

Now when the user clicks an item in the spinner, you can look up the corresponding age and key based on the index of the selected item:


spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
        String selected = nameList.get(position).toString();
        Log.e("Clicked:",""+selected);
        String fAge = ageList.get(position).toString();        
        tv_1st.setText(fAge);
    }

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

Upvotes: 1

Related Questions