简体   繁体   English

如何在Firebase中的集合下的文档中添加多个唯一值

[英]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.

相关问题 如何将文档添加到事务内的 Firebase 集合? - How can I add a document to a Firebase collection inside a transaction? 如何在 Firestore 中获取文档中的集合数量? - How can I get the number of collections inside a document in Firestore? 将文档添加到多个文件夹(集合) - Add a document to multiple folders (collections) Java集合-如何在集合中添加多个值? - Java collections - How to add multiple values in the collection? 如何从两个不同的 db.collections 中获取数据并将它们合并到 Kotlin-Firebase 中的一个类中? - How can I take data from two different db.collections and unite them under one class in Kotlin-Firebase? 如何向 Firebase - Android 中的现有列表添加值 - How can I add values to existing list in Firebase - Android 如何将多个图像的 downloadUrl 存储到 Firebase Firestore ? 无法在 Firestore 文档中的多个字段中创建和存储 - How do I store downloadUrl of multiple images to the Firebase Firestore ? Unable to create and store inside multiple fields inside Firestore document 如何更新文档内的arrayList? - How can I update arrayList inside document? 如何将元素添加到不同类型的 Java 集合的 PriorityQueue 中? - How can i add elements to a PriorityQueue of Java Collections that are of different type? 如何在SQL的一栏中添加多个值? - How can i add multiple values into one column in sql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM