使用 Android Studio 和 Firebase 在 Spinner 中获取其选定子值的键

我试图通过允许用户从微调器中进行选择来获取某个子值的键。

Spinner 将“Product_Name”值作为其选择。通过选择一个,程序应该获取其密钥并使用它来获取另一个子值以供其他用途。

例子:

产品-> -LoUXnfUCEj4k4A3dkte-> 产品名称:“牛排”

通过在微调器中选择“Steak”,我必须得到“-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);


我怎样才能得到“-LoUXnfUCEj4k4A3dkte”?


注意: -LoUXnfUCEj4k4A3dkte 是随机生成的。


精慕HU
浏览 95回答 2
2回答

MMTTMM

用于.getKey()获取快照的密钥,例如:for(final DataSnapshot snapshot : dataSnapshot.getChildren()) {&nbsp; &nbsp; if (snapshot.child("Product_Name").getValue(String.class).equals("Steak")){&nbsp; &nbsp; &nbsp; &nbsp; String theKey = snapshot.getKey(); //This will return -LoUXnfUCEj4k4A3dkte&nbsp; &nbsp; }}将返回该引用处快照的键。

潇潇雨雨

找到了解决方案。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) {        }    });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java