5

pls help me with this tinny issue

I've this android code:

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

then, i use firestore to save my data:

Map<String, Object> service = new HashMap<>();
            service.put("full_name", full_name.getText().toString());
            service.put("Services_names", Collections.singletonList(serviceNameList));

            firestore.collection("service").document("new_service")
                    .set(service)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                            public void onSuccess(Void aVoid) {
                                Toast.makeText(getApplicationContext(), "Success..!",Toast.LENGTH_SHORT).show();
                            }
            })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                            public void onFailure(@NonNull Exception e) {
                                String error = e.getMessage();
                                Toast.makeText(getApplicationContext(), "Error: " + error,Toast.LENGTH_SHORT).show();
                        }
            });

all works fine when i comment or delete this line:

service.put("Services_names",Collections.singletonList(serviceNameList));

But when i enable it again my app fail... I want to know what can i do for solve this...

Thank you..!!!!

btw, this is my logcat

2019-07-08 23:03:04.943 com.comas.app E: FATAL EXCEPTION: main
Process: com.comas.app, PID: 30989
java.lang.IllegalArgumentException: Invalid data. Nested arrays are not supported
    at com.google.firebase.firestore.core.UserData$ParseContext.createError(com.google.firebase:firebase-firestore@@19.0.2:293)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:270)
    at com.google.firebase.firestore.UserDataConverter.parseList(com.google.firebase:firebase-firestore@@19.0.2:307)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:272)
    at com.google.firebase.firestore.UserDataConverter.parseMap(com.google.firebase:firebase-firestore@@19.0.2:294)
    at com.google.firebase.firestore.UserDataConverter.parseData(com.google.firebase:firebase-firestore@@19.0.2:250)
    at com.google.firebase.firestore.UserDataConverter.convertAndParseDocumentData(com.google.firebase:firebase-firestore@@19.0.2:230)
    at com.google.firebase.firestore.UserDataConverter.parseSetData(com.google.firebase:firebase-firestore@@19.0.2:83)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:175)
    at com.google.firebase.firestore.DocumentReference.set(com.google.firebase:firebase-firestore@@19.0.2:154)
    at com.comas.app.PopUpActivity$2.onClick(PopUpActivity.java:173)
    at android.view.View.performClick(View.java:6663)
    at android.view.View.performClickInternal(View.java:6635)
    at android.view.View.access$3100(View.java:794)
    at android.view.View$PerformClick.run(View.java:26199)
    at android.os.Handler.handleCallback(Handler.java:907)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:216)
    at android.app.ActivityThread.main(ActivityThread.java:7593)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)2019-07-08 23:03:04.955 ? E: FaultDetect: DUMPTOOL_PRINTF return.
4
  • Didn't get your problem exactly what you want ? Commented Jul 9, 2019 at 4:22
  • It is clearly written Invalid data. Nested arrays are not supported. You should try to save it another way like json etc Commented Jul 9, 2019 at 4:24
  • Check this Commented Jul 9, 2019 at 4:59
  • What are u trying to insert? Is it a file or data? If you are trying to insert data, better use firebase realtime database, or it is a file, there is some other way Commented Jul 9, 2019 at 5:02

2 Answers 2

0

If you want to store an ArrayList data into Firestore, first iterate the entire ArrayList. Then, pass that variable only into HashMap.

private ArrayList serviceNameList = new ArrayList();

serviceNameList.add("windows");
serviceNameList.add("linux");
serviceNameList.add("mac");

Map<String, Object> service = new HashMap<>();
service.put("full_name", full_name.getText().toString());
service.put("Services_names", serviceNameList);

Kotlin

val serviceNameList = ArrayList<Any>()

serviceNameList.add("windows")
serviceNameList.add("linux")
serviceNameList.add("mac")

val service: MutableMap<String, Any> = HashMap()
service["full_name"] = full_name.getText().toString()
service["Services_names"] = serviceNameList
Sign up to request clarification or add additional context in comments.

Comments

0

Here is the code to upload a file to firestore

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReference();

private void uploadImage() {
        if (filePath != null) { //path of the file you want to upload
            final ProgressDialog dialog = new ProgressDialog(this);
            dialog.setTitle("Uploading...");
            dialog.show();

            StorageReference ref = storageReference.child("images/" + UUID.randomUUID().toString());
            ref.putFile(filePath)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            dialog.dismiss();
                            System.out.println("image uploaded path " + taskSnapshot.getUploadSessionUri());
                            Toast.makeText(ListActivity.this, R.string.upload_success, Toast.LENGTH_LONG).show();
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            dialog.dismiss();
                            Toast.makeText(ListActivity.this, R.string.upload_failure, Toast.LENGTH_LONG).show();
                        }
                    })
                    .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                            double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot
                                    .getTotalByteCount());
                            dialog.setMessage("Uploaded "+(int)progress+"%");
                        }
                    });

        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.