通过onDatachage保存从检索到的数据的微调器中选择的项的键

时间:2017-08-08 09:09:31

标签: android firebase

我可以从我的数据库中检索数据firebase spinner已满,但我无法保存选中的项目我放线

uaselected = uaSpinner.getSelectedItem().toString().trim();

在OnData更改后保存选中的项但它返回null,因为它在函数OnData更改任何帮助之前首先执行

2 个答案:

答案 0 :(得分:1)

使用此方法。

  spncountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    country_data=spncountry.getSelectedItem().toString();
                                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

答案 1 :(得分:0)

  databasereference = database.getReference();
    mAuth = FirebaseAuth.getInstance();
    areaSpinner = (Spinner) findViewById(R.id.spinneretab);

      databasereference.child("UA_unity").addValueEventListener(new com.google.firebase.database.ValueEventListener() {
                @Override
                public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                    final List<String> etab = new ArrayList<String>();
                    final List<String> keyunity = new ArrayList<String>();
                   // uaselected = areaSpinner.getSelectedItem().toString().trim();


                    for (DataSnapshot child: dataSnapshot.getChildren()) {
                        nameua = (String) child.child("nom_UA").getValue();
                        keyua = child.getKey();
                      //  keyunity.add(keyua);
                      //  databasereference.child("Users").child("UA_ID").setValue(keyua);
                        etab.add(nameua);
                    }
                    ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(add_info_user.this, android.R.layout.simple_spinner_item, etab);
                    areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    areaSpinner.setAdapter(areasAdapter);
                    uaselected = areaSpinner.getSelectedItem().toString().trim();


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }