亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 Android Studio 和 Firebase 在 Spinner 中獲取其選定子值的鍵

使用 Android Studio 和 Firebase 在 Spinner 中獲取其選定子值的鍵

精慕HU 2023-07-28 16:17:45
我試圖通過允許用戶從微調器中進行選擇來獲取某個子值的鍵。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 是隨機生成的。
查看完整描述

2 回答

?
MMTTMM

TA貢獻1869條經驗 獲得超4個贊

用于.getKey()獲取快照的密鑰,例如:


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

    if (snapshot.child("Product_Name").getValue(String.class).equals("Steak")){

        String theKey = snapshot.getKey(); //This will return -LoUXnfUCEj4k4A3dkte

    }

}

將返回該引用處快照的鍵。


查看完整回答
反對 回復 2023-07-28
?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

找到了解決方案。


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) {

? ? ? ? }

? ? });


查看完整回答
反對 回復 2023-07-28
  • 2 回答
  • 0 關注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號