我试图通过允许用户从微调器中进行选择来获取某个子值的键。
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 是随机生成的。
MMTTMM
潇潇雨雨
相关分类