将表单数据添加到firebase数据库

时间:2017-07-18 10:32:21

标签: android firebase firebase-realtime-database

我对编码很新。我正在尝试使用层次结构将数据保存到firebase数据库:作业ID(作业1,作业2等) - >工作参数(日期,优先级等)

这是我正在使用的表格:

submission form

这是我原始代码中的firebase层次结构

firebase structure

这是我的代码:

package com.example.myname.todo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

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

public class JobForm extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_job_form);

    // Connect to the Firebase database
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    // Add items via the Button and EditText at the bottom of the window.
    final EditText jobNum = (EditText) findViewById(R.id.job_id);
    final EditText jobDate = (EditText) findViewById(R.id.date_now);
    final EditText jobPri = (EditText) findViewById(R.id.job_imp);
    final EditText jobDeet = (EditText) findViewById(R.id.job_dis);
    final EditText jobMch1 = (EditText) findViewById(R.id.first_machine);
    final EditText jobMch2 = (EditText) findViewById(R.id.second_machine);
    // Get a reference to the child items it the database


    final DatabaseReference jobID = database.getReference("Job_ID");
    final DatabaseReference dateStart = database.getReference("Start_Date");
    final DatabaseReference jobImp = database.getReference("Job_Priority");
    final DatabaseReference jobDes = database.getReference("Job_Description");
    final DatabaseReference jobMach1 = database.getReference("First_Machine");
    final DatabaseReference jobMach2 = database.getReference("Second_Machine");



    final Button but_sub = (Button) findViewById(R.id.but_sub);

    but_sub.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            // Create a new child with a auto-generated ID.
            DatabaseReference childRef = jobID.push();
            // Set the child's data to the value passed in from the text box.
            childRef.setValue(jobNum.getText().toString());
            DatabaseReference childRef2 = dateStart.push();
            childRef2.setValue(jobDate.getText().toString());
            DatabaseReference childRef3 = jobImp.push();
            childRef3.setValue(jobPri.getText().toString());
            DatabaseReference childRef4 = jobDes.push();
            childRef4.setValue(jobDeet.getText().toString());
            DatabaseReference childRef5 = jobMach1.push();
            childRef5.setValue(jobMch1.getText().toString());
            DatabaseReference childRef6 = jobMach2.push();
            childRef6.setValue(jobMch2.getText().toString());
        }
    });


}

}

我想知道该做什么,以便用户输入的每个作业在数据库中创建一个具有其特定信息的Child。所以喜欢:

  • Job1(作业1信息,展开时)
  • job2(扩展后的工作2信息)

感谢

1 个答案:

答案 0 :(得分:1)

好吧,在我看来,在学习firebase或Firebase如何工作时你搞砸了,你不需要每次都声明DatabaseReference,你只需要为表调用一次。见:

final EditText jobNum = (EditText) findViewById(R.id.job_id);
final EditText jobDate = (EditText) findViewById(R.id.date_now);
final EditText jobPri = (EditText) findViewById(R.id.job_imp);
final EditText jobDeet = (EditText) findViewById(R.id.job_dis);
final EditText jobMch1 = (EditText) findViewById(R.id.first_machine);
final EditText jobMch2 = (EditText) findViewById(R.id.second_machine);
// Get a reference to the child items it the database


final DatabaseReference job = database.getReference("JOBS");
final Button but_sub = (Button) findViewById(R.id.but_sub);\`but_sub.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       // you must creat a job class 
       // asumming you did it will have the toString()method in ever parmeteter in the constracter 
       Job job = new Job(jobNum.getText().toString(),...exc);
       job.child(jobNum).setValue(job);

//现在,你的Firebase数据库中应该有一个名为job的节点,它的关键是jobNum,值是job。

    }
});


  }`

希望它有所帮助:)