Reputation: 83
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.
Upvotes: 5
Views: 1165
Reputation: 891
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
Upvotes: 0
Reputation: 1345
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+"%");
}
});
}
}
Upvotes: 0