2 回答

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
}
}
將返回該引用處快照的鍵。

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) {
? ? ? ? }
? ? });
添加回答
舉報