微调器值未存储在Firebase数据库中

时间:2018-11-03 11:06:29

标签: android firebase firebase-realtime-database spinner

我有一个使用textviews和spinners将数据存储在firebase中的应用程序,textviews的值已正确保存,但spinners值未显示在数据库中,而是存储了一些随机值。 My firebase database structure

这是我的候选人1类,它将数据写入Firebase:

    package package_name;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.text.TextUtils;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;

    import com.firebase.client.Firebase;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;

    public class candidate1 extends AppCompatActivity{

        private EditText editText1, editText2, editText3;
        private Spinner  spinner1 , spinner2;
        private Button submmit;
        DatabaseReference ref;
        FirebaseDatabase database;
        candidate candi;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_candidate);
            Firebase.setAndroidContext(this);

            editText1 = (EditText) findViewById(R.id.editText1);
            editText2 = (EditText) findViewById(R.id.editText2);
            editText3 = (EditText) findViewById(R.id.editText3);

            spinner1 = (Spinner) findViewById(R.id.spinner1);
            spinner2 = (Spinner) findViewById(R.id.spinner2)

            ArrayAdapter<CharSequence> adapter1= ArrayAdapter.createFromResource(this,R.array.program, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner1.setAdapter(adapter1);

            ArrayAdapter<CharSequence> adapter2= ArrayAdapter.createFromResource(this,R.array.session, android.R.layout.simple_spinner_item);
            adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner2.setAdapter(adapter2);

            database = FirebaseDatabase.getInstance();
            ref = database.getReference("candidate");
            candi = new candidate();
            submmit=(Button) findViewById(R.id.submmit);
            submmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                            addCandidate();
                            }
                            Toast.makeText(candidate1.this,"Data inserted successfully...",Toast.LENGTH_LONG).show();
                        }
            });
        }
        private void getValues(){
            candi.setName(editText1.getText().toString());
            candi.setRegNo(editText2.getText().toString());
            candi.setCatagory(editText3.getText().toString());
            candi.setProgram(spinner1.toString());
            candi.setSession(spinner2.toString());
        }
      private void addCandidate(){
          ref= FirebaseDatabase.getInstance().getReference().child("candidate").push();
          String name= editText1.getText().toString().trim();
          String regNo= editText2.getText().toString().trim();
          String catagory= editText3.getText().toString().trim();
          String session= spinner2.toString().trim();
          String program= spinner1.toString().trim();
          String id=ref.push().getKey();
          if((id.equals(id))&&(!TextUtils.isEmpty(name))&&(!TextUtils.isEmpty(regNo))&&(!TextUtils.isEmpty(catagory))&&(!TextUtils.isEmpty(session))&&(!TextUtils.isEmpty(program))) {
              getValues();
              ref.child(id).push();
              ref.child("Name").setValue(name);
              ref.child("RegNo").setValue(regNo);
              ref.child("Category").setValue(catagory);
              ref.child("Program").setValue(program);
              ref.child("Session").setValue(session);         
              Toast.makeText(candidate1.this,"Datainserted...",
Toast.LENGTH_LONG).show();
          }else{
              Toast.makeText(candidate1.this,"Data insertion Failed...",Toast.LENGTH_LONG).show();
          }
      }
    }

我使用了两个微调器 program session ,并在 string.XML 中定义了它们的数组。请帮助我,我不知道我在哪里犯了错误。

1 个答案:

答案 0 :(得分:0)

您的代码没有任何错误 为了从微调器中获取价值,您需要使用

以下内容:

user.setProblem(spinner_problem.getSelectedItem().toString().trim());

您必须使用getSelectedItem(),这将调用您存储在string.xml文件中的值,其中将显示所选项目。