我想要一个类似image的数据库,我可以在其中为每个创建一次的文档添加多个子集合。例如。输入用户详细信息后,会在文档中创建一个子集合(机构部门),这将进一步在同一文档中创建子集合(建议)。我正在使用不同的Java类来获取各种详细信息。例如:我封闭了两个类,即sports_detail和建议:
package com.example.admin.howdy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class sportpage extends AppCompatActivity {
CheckBox kabaddi,volley,basket,other,cricket,race;
EditText textView,contactsport;
Button button3;
FirebaseFirestore mfirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sportpage);
kabaddi=(CheckBox)findViewById(R.id.checkBox2);
volley=(CheckBox)findViewById(R.id.checkBox3);
basket=(CheckBox)findViewById(R.id.checkBox5);
race=(CheckBox)findViewById(R.id.checkBox4);
cricket=(CheckBox)findViewById(R.id.checkBox6);
other=(CheckBox)findViewById(R.id.checkBox7);
textView=(EditText) findViewById(R.id.sportcontestant);
contactsport=(EditText) findViewById(R.id.sportcontact);
mfirestore = FirebaseFirestore.getInstance();
button3 = (Button) findViewById(R.id.button);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendsportdetail();
Intent i=new Intent(sportpage.this,Thanks.class);
startActivity(i);
}
});
}
private void sendsportdetail()
{
Map<String, String> usmap=new HashMap<>();
String name=textView.getText().toString().trim();
String contact=contactsport.getText().toString().trim();
String text="";
if (kabaddi.isChecked()){
text = kabaddi.getText().toString();
usmap.put("Category", text);
}
if (basket.isChecked()){
text = basket.getText().toString();
usmap.put("Category", text);
}
if (volley.isChecked()){
text = volley.getText().toString();
usmap.put("Category", text);
}
if (cricket.isChecked()){
text = cricket.getText().toString();
usmap.put("Category", text);
}
if (race.isChecked()){
text = race.getText().toString();
usmap.put("Category", text);
}
if (other.isChecked()){
text = other.getText().toString();
usmap.put("Category", text);
}
if(!kabaddi.isChecked()&&!race.isChecked()&&!cricket.isChecked()&&!other.isChecked()&&!basket.isChecked()&&!volley.isChecked())
Toast.makeText(getBaseContext(),"Please select an option",Toast.LENGTH_LONG).show();
usmap.put("Name",name);
usmap.put("Contact",contact);
//String str=mfirestore.collection("MYPrince").document().getId();
//Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG).show();
mfirestore.collection("MYPrince").document().collection("Sports_Registration").add(usmap);
}
}
package com.example.admin.howdy;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
/**
* Created by admin on 01-Aug-18.
*/
public class third extends Activity {
Button submitbutton;
EditText name, email, suggestion;
FirebaseFirestore myfirestore;
/*FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference mRootReference=firebaseDatabase.getReference();
DatabaseReference namereference=mRootReference.child("Name");
DatabaseReference emailreference=mRootReference.child("Email");
DatabaseReference feedbackreference=mRootReference.child("Feedback");
//DatabaseReference databaseReference;
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
myfirestore= FirebaseFirestore.getInstance();
name = (EditText) findViewById(R.id.editText3);
email = (EditText) findViewById(R.id.editText);
suggestion = (EditText) findViewById(R.id.editText2);
submitbutton = (Button) findViewById(R.id.button3);
submitbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (name.getText().toString().length()>0 && email.getText().toString().length()>0 && suggestion.getText().toString().length()>0) {
sendsuggestion();
Intent myintent3 = new Intent(third.this, second.class);
startActivity(myintent3);
} else {
Toast.makeText(getBaseContext(), "Please Enter all the fields!", Toast.LENGTH_LONG).show();
}
}
});
}
private void sendsuggestion()
{
Map<String, String> usmap=new HashMap<>();
String username=name.getText().toString().trim();
usmap.put("Name",username);
//namereference.setValue(username);
String useremail=email.getText().toString().trim();
usmap.put("Email",useremail);
//emailreference.setValue(useremail);
String usersuggestion=suggestion.getText().toString();
usmap.put("Suggestion",usersuggestion);
//feedbackreference.setValue(usersuggestion);
String str=myfirestore.collection("MYPrince").document().getId();
myfirestore.collection("MYPrince").document(str).collection("Suggestions").add(usmap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getBaseContext(),"Your suggestion has been sent. Thanks for your time.",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error=e.getMessage();
Toast.makeText(getBaseContext(),"Error"+error,Toast.LENGTH_LONG).show();
}
});
}
}
但是在完成所有这些操作之后,我得到了如下数据库: image2