我尝试用户填写表单,一旦用户点击“提交”#39;按钮,数据将存储在Firebase中的新密钥中。新密钥只有在“提交”后才会存在。单击按钮将数据传递到Firebase。
这是我目前的代码:
CustomersDetail.java
public class CustomersDetail extends AppCompatActivity {
FirebaseDatabase database;
DatabaseReference customerDetails;
TextView editPhone, editName, editDOB, editEmail;
Button btnSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customers_detail);
//Firebase
database = FirebaseDatabase.getInstance();
customerDetails = database.getReference("Customer Details");
btnSubmit = (Button)findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Click 'Submit'
//Submit to Firebase
//Create new Request
CustomerDetail custDetail = new CustomerDetail(
editPhone.getText().toString(),
editName.getText().toString(),
editDOB.getText().toString(),
editEmail.getText().toString()
);
//Submit to Firebase
customerDetails.child(String.valueOf(System.currentTimeMillis()))
.setValue(custDetail);
}
});
}
}
activity_customers_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
android:orientation="vertical"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/editPhone"
android:hint="Phone Number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/black"
android:text=""
android:textColor="@android:color/black"
android:textSize="24sp"
android:inputType="phone"
app:met_baseColor="@android:color/black"
app:met_floatingLabel="highlight"
app:met_maxCharacters="15"
app:met_primaryColor="@android:color/black"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/editName"
android:hint="Full Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/black"
android:text=""
android:textColor="@android:color/black"
android:textSize="24sp"
android:inputType="text"
app:met_baseColor="@android:color/black"
app:met_floatingLabel="highlight"
app:met_maxCharacters="50"
app:met_primaryColor="@android:color/black"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/editDOB"
android:hint="Date of Birth (DD/MM/YYYY)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/black"
android:text=""
android:textColor="@android:color/black"
android:textSize="24sp"
android:inputType="date"
app:met_baseColor="@android:color/black"
app:met_floatingLabel="highlight"
app:met_maxCharacters="30"
app:met_primaryColor="@android:color/black"
app:met_singleLineEllipsis="true"
/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="@+id/editEmail"
android:hint="Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@android:color/black"
android:text=""
android:textColor="@android:color/black"
android:textSize="24sp"
android:inputType="textEmailAddress"
app:met_baseColor="@android:color/black"
app:met_floatingLabel="highlight"
app:met_maxCharacters="30"
app:met_primaryColor="@android:color/black"
app:met_singleLineEllipsis="true"
/>
</LinearLayout>
<info.hoang8f.widget.FButton
android:id="@+id/btnSubmit"
android:text="Submit"
android:textColor="@android:color/white"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
app:buttonColor="@color/button"
app:cornerRadius="4dp"
/>
</RelativeLayout>
答案 0 :(得分:0)
我有点困惑,好像你在使用TextView或EdiTtext一样?
无论您使用什么,都需要将其与xml文件中的内容相关联。
将此代码放在onClick()方法中,或者创建一个额外的方法,在按钮的onClick()中调用该方法,然后可以在外面编写方法的定义。
向Firebase提交数据的代码可以通过以下方式完成:
customerDetails.child(String.valueOf(System.currentTimeMillis()))
.setValue(custDetail);
//TO READ DATA
customerDetails.child(String.valueOf(System.currentTimeMillis())).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
CustomerDetail cd = dataSnapshot.getValue(CustomerDetail.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d(TAG, "Failed To Read data", databaseError.toException());
}
});