简体   繁体   中英

Getting the key of its selected child value in a Spinner using Android Studio and Firebase

I'm trying to get the key of a certain child value by allowing the user to choose from a Spinner.

The Spinner has "Product_Name" values as its choices. By choosing one, the program should get its key and use it to get another child value for other uses.

Example:

PRODUCTS-> -LoUXnfUCEj4k4A3dkte-> Product_Name:"Steak"

By choosing "Steak" in the Spinner, I have to get "-LoUXnfUCEj4k4A3dkte"

    databaseRefSelectItem = FirebaseDatabase.getInstance().getReference("PRODUCTS");

    final DatabaseReference mDatabase = databaseRefSelectItem;
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
            //We create an array list to hold the values brought from the database and show them in the spinner
            final List<String> titleList = new ArrayList<String>();

            for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {

                titleProduct = snapshot.child("Product_Name").getValue(String.class);

                //populate the spinner with that array list
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AddTransactionActivity.this, android.R.layout.simple_spinner_item, titleList);
                arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                selectProduct.setAdapter(arrayAdapter);

                titleList.add(titleProduct);

                //Click event for each spinner element
                selectProduct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        //pass the reference from that value into another snapshot in order to query those values, here you need to get your node id and inside just get your number , name and so on
                        selectItem = titleList.get(i);

                        mDatabase.child(selectItem).addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(final DataSnapshot dataSnapshot2) {

                                key = dataSnapshot2.getKey();

                                currentItemStock = dataSnapshot2.child(key).child("Current_Stock").getValue(String.class);
                                currentStk.setText(key);

                            }

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

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

How can I get "-LoUXnfUCEj4k4A3dkte"?

Notes: -LoUXnfUCEj4k4A3dkte is randomly generated.

Use .getKey() to get the key of the snapshot like:

for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
    if (snapshot.child("Product_Name").getValue(String.class).equals("Steak")){
        String theKey = snapshot.getKey(); //This will return -LoUXnfUCEj4k4A3dkte
    }
}

Will return key of the snapshot at that reference.

Found a solution. Thank you as well to Yash Krishan for the code snippet :).

databaseRefSelectItem = FirebaseDatabase.getInstance().getReference("PRODUCTS");

    final DatabaseReference mDatabase = databaseRefSelectItem;
    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull final DataSnapshot dataSnapshot) {
            //We create an array list to hold the values brought from the database and show them in the spinner
            final List<String> titleList = new ArrayList<String>();

            for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                titleProduct = snapshot.child("Product_Name").getValue(String.class);

                //populate the spinner with that array list
                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(AddTransactionActivity.this, android.R.layout.simple_spinner_item, titleList);
                arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                selectProduct.setAdapter(arrayAdapter);

                titleList.add(titleProduct);

                //Click event for each spinner element
                selectProduct.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        //pass the reference from that value into another snapshot in order to query those values, here you need to get your node id and inside just get your number , name and so on
                        selectItem = titleList.get(i);

                        if (titleProduct.equals(selectItem)){
                            key = dataSnapshot.child(selectItem).getKey(); //This will return -LoUXnfUCEj4k4A3dkte
                        }

                        mDatabase.addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull final DataSnapshot dataSnapshot2) {

                                for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {
                                    if (snapshot.child("Product_Name").getValue(String.class).equals(selectItem)){
                                        key = snapshot.getKey().toString();
                                    }
                                    keyholder = dataSnapshot.child(key).child("Current_Stock").getValue(String.class);
                                }
                                currentStk.setText(keyholder);
                            }

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

                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });
            }
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM