2017-07-18 117 views
-2

我完全不熟悉編碼。我想數據火力數據庫層次結構保存:作業ID(作業1,作業2,等) - >作業參數(如日期,優先級等)將表單數據添加到firebase數據庫

這是我使用的形式:

submission form

這是我的火力層次從我的原代碼

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()); 
     } 
    }); 


} 

}

我想知道該怎麼做才能讓用戶輸入的每個作業在數據庫中創建一個孩子,其具體信息。所以像:

  • 作業1(作業1的信息,展開後)
  • 作業2(工作展開後2信息)

感謝

回答

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。

} 
}); 


    }` 

希望它能幫助:)

+0

感謝您的答覆,我會更新你的結果,當我得到機會實現更改 –

+0

它的工作非常感謝您的幫助! 。 ps如果我想添加更多的節點,我是否需要創建另一個類? –

+0

很好聽,它的工作原理,如果你需要爲同一個類創建其他節點,只需使用'DatabaseReference作業',如果它是爲一個不同的類定義了一個新的'DatabaseReference' – SalmanALharbi