如何在Firebase数据库中插入日期作为子名称并添加数组

时间:2018-02-27 16:38:53

标签: android arrays firebase datetime firebase-realtime-database

我正在尝试创建一个基本应用,向Firebase数据库添加信息。我有三个字段和一个日期。我想从EditText中添加日期作为Firebase中的子名称,而不是在代码中写入它。如下图所示:

enter image description here

我现在拥有的代码是一次又一次地生成相同的表(如左侧),因为我无法设法构建数组。因此,当" BUTTON"时,我正在寻求帮助来生成这样的数组。单击按钮(见下图)。

enter image description here

除了生成一个数组之外,我希望每个孩子都能代替字符串" Date&#34 ;,日期来自布局中的EditText(图片的右侧)。

到目前为止,我有这个基本代码:

MainActivity.java



import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {

  EditText date, text_field2, text_field3, text_field4;
  Button send_button;
  User user;
  FirebaseDatabase database; // = FirebaseDatabase.getInstance();
  DatabaseReference myRef; // = database.getReference();



  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    date = (EditText) findViewById(R.id.date);
    text_field2 = (EditText) findViewById(R.id.field2);
    text_field3 = (EditText) findViewById(R.id.field3);
    text_field4 = (EditText) findViewById(R.id.field4);
    send_button = (Button) findViewById(R.id.button);
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("User");
    user = new User();
  }

  private void getValues() {
    user.setDate(date.getText().toString());
    user.setField2(text_field2.getText().toString());
    user.setField3(text_field3.getText().toString());
    user.setField4(text_field4.getText().toString());
  }

  public void Send(View view) {
    myRef.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot dataSnapshot) {
        getValues();
        /*String UID = Array.getUID();
        myRef.child(UID).setValue(user);*/
        myRef.child("Date").setValue(user);
        Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG);
      }

      @Override
      public void onCancelled(DatabaseError databaseError) {

      }
    });
  }

}




User.java



//User has to be the same as the first folder
public class User {
  private String Date, Field2, Field3, Field4, UID;

  public User() {}


  public User(String date, String field2, String field3, String field4, String UID) {
    Date = date;
    Field2 = field2;
    Field3 = field3;
    Field4 = field4;
    this.UID = UID;

  }

  public String getDate() {
    return Date;
  }

  public void setDate(String date) {
    Date = date;
  }

  public String getField2() {
    return Field2;
  }

  public void setField2(String field2) {
    Field2 = field2;
  }

  public String getField3() {
    return Field3;
  }

  public void setField3(String field3) {
    Field3 = field3;
  }

  public String getField4() {
    return Field4;
  }

  public void setField4(String field4) {
    Field4 = field4;
  }

  public String getUID() {
    return UID;
  }

  public void setUID(String UID) {
    this.UID = UID;
  }
}




activity_main.xml中



<< ? xml version = "1.0"
encoding = "utf-8" ? >
  <
  android.support.constraint.ConstraintLayout xmlns : android = "http://schemas.android.com/apk/res/android"
xmlns: app = "http://schemas.android.com/apk/res-auto"
xmlns: tools = "http://schemas.android.com/tools"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
tools: context = ".databaseandstorage.MainActivity" >

  <
  LinearLayout
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical" >

  <
  EditText
android: id = "@+id/date"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: ems = "10"
android: inputType = "date"
android: text = "YYYY.MM.DD.HH.MM" / >

  <
  EditText
android: id = "@+id/field2"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: ems = "10"
android: inputType = "textPersonName"
android: text = "Field 2" / >

  <
  EditText
android: id = "@+id/field3"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: ems = "10"
android: inputType = "textPersonName"
android: text = "Field 3" / >

  <
  EditText
android: id = "@+id/field4"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: ems = "10"
android: inputType = "textPersonName"
android: text = "Field 4" / >

  <
  Button
android: id = "@+id/button"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: onClick = "Send"
android: text = "Button" / >


  <
  /LinearLayout> <
  /android.support.constraint.ConstraintLayout>
&#13;
&#13;
&#13;

我确实尝试制作一个阵列,但它完全失败,因为当我按下按钮时,它正在生成随机文本配置而没有停止。它填补了数千行。该数组的代码是:

&#13;
&#13;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;



public class Array {
  public static String getUID() {
    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").push();
    String[] urlArray = databaseReference.toString().split("/");
    return urlArray[urlArray.length - 1];
  }

}
&#13;
&#13;
&#13;

所以有人可以帮助我: 1.将数据库中的文本作为数组添加,每次单击&#34;按钮&#34; 2.使用EditText中的日期作为Firebase数据库中每个子项的名称。

2 个答案:

答案 0 :(得分:2)

主要的想法是你必须更新这些行:

 public void onDataChange(DataSnapshot dataSnapshot) {
    getValues();
    /*String UID = Array.getUID();
    myRef.child(UID).setValue(user);*/
    myRef.child("Date").setValue(user);
    Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG);
  }

这样的事情:

 public void onDataChange(DataSnapshot dataSnapshot) {
    getValues();
    String dateValue = "20180606_6PM";
    myRef.child(dateValue).setValue(user);
    Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG);
  }

你必须以某种方式计算新的dateValue字符串

而不是在onDataChange方法中执行它,你应该在buttonClickListener中执行它

 send_button.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
             String dateValue = "20180606_6PM";
    myRef.child(dateValue).setValue(user);
    Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG);
         }
     });

答案 1 :(得分:2)

@Christopher Rivera 有最佳答案使用此

   DateFormat format = new SimpleDateFormat("YYYYMMdd_hhmm a");
   String date = format.format(new Date());  

   long d = System.currentTimeMillis(); 
   String date = format.format(new Date(d));  
// result will be 20180227_1011 PM

或使用android日期选择器link