[英]How Can I add multiple unique values inside a document under collections in the Firebase
I'm trying to add multiple roll number(unique) inside a document field inside a collection in a Cloud Firestore. 我正在尝试在Cloud Firestore中的集合内的文档字段中添加多个卷号(唯一)。 How can I achieve it plus I wanted to restrict the user to not enter more than 5 values.
我怎样才能实现它,我想限制用户不要输入超过5个值。 Can I get some help out here?
我可以在这里得到一些帮助吗?
Groupformation_fragment.java Groupformation_fragment.java
public class groupformation_fragment extends Fragment {
private Button submit;
EditText ET1,ET2,ET3,ET4,ET5;
private FirebaseFirestore mfirestore;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_groupformation,null);
ET1=(EditText)v.findViewById(R.id.ET1);
ET2=(EditText)v.findViewById(R.id.ET2);
ET3=(EditText)v.findViewById(R.id.ET3);
ET4=(EditText)v.findViewById(R.id.ET4);
ET5=(EditText)v.findViewById(R.id.ET5);
submit=(Button)v.findViewById(R.id.submit);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String roll1=ET1.getText().toString();
String roll2=ET2.getText().toString();
String roll3=ET3.getText().toString();
String roll4=ET4.getText().toString();
String roll5=ET5.getText().toString();
Map<String,String> usermap= new HashMap<>();
usermap.put("Student1",roll1);
usermap.put("Student2",roll2);
usermap.put("Student3",roll3);
usermap.put("Student4",roll4);
usermap.put("Student5",roll5);
mfirestore.collection("group").add(usermap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getActivity(), "Group Has been Registered For The Project", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
String error=e.getMessage();
Toast.makeText(getActivity(), "Error :"+error, Toast.LENGTH_SHORT).show();
}
});
}
});
}
} }
fragement_groupformation.xml fragement_groupformation.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/bkg3"
android:layout_height="match_parent">
<EditText
android:id="@+id/ET1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default"/>
<EditText
android:id="@+id/ET2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<EditText
android:id="@+id/ET5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Roll number"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:inputType="number"
android:textColorHint="@color/common_google_signin_btn_text_dark_default" />
<Button
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="40dp"
android:background="@color/common_google_signin_btn_text_dark_default"
android:text="Submit"/>
</LinearLayout>
Logcat Error shown 显示Logcat错误
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.kiit.projectmanager, PID: 7163
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.firestore.CollectionReference com.google.firebase.firestore.FirebaseFirestore.collection(java.lang.String)' on a null object reference
at com.kiit.projectmanager.groupformation_fragment$1.onClick(groupformation_fragment.java:62)
at android.view.View.performClick(View.java:5215)
at android.view.View$PerformClick.run(View.java:21196)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
I/Process: Sending signal. PID: 7163 SIG: 9
Application terminated.
Beeing a NullPointerException
, it means that you haven't initialized the mfirestore
object anywhere in your code. 使用
NullPointerException
,这意味着您尚未在代码中的任何位置初始化mfirestore
对象。 To solve this, please change the following line of code: 要解决此问题,请更改以下代码行:
private FirebaseFirestore mfirestore;
to 至
private FirebaseFirestore mfirestore = FirebaseFirestore.getInstance();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.