I am new to android development. I have a question. Can I add data to the same field with different names in the Cloud Firestore in Android Studio? For example, the user enters his name and selects the area in the Spinner and clicks "Add." The data should fall into one collection and field in the Cloud Firestore. The code below shows how to add data to different collections:

btn_setData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, String> nameMap = new HashMap<>(); Map<String, String> spinnerMap = new HashMap<>(); nameMap.put("name", et_setName.getText().toString()); spinnerMap.put("district", sp_selectDistrict.getSelectedItem().toString()); mFirestore.collection("users").document().set(nameMap).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if(task.isSuccessful()){ Toast.makeText(SelectLocationActivity.this, "Запись прошла успешно", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(SelectLocationActivity.this, "Запись не удалась", Toast.LENGTH_SHORT).show(); } } }); mFirestore.collection("users_district").document().set(spinnerMap).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if(task.isSuccessful()){ Toast.makeText(SelectLocationActivity.this, "Запись прошла успешно", Toast.LENGTH_SHORT).show(); } } }); } }); 

Is it possible to combine these two actions? Or with the type "Object" in the database when creating a field. Thank.

    1 answer 1

    The issue is resolved. In the handler, instead of

     Map <String, String> 

    specify that we want to put the parameters in the object type

     Map<String, Object> 

    in Cloud Firestore. We define one HashMap and insert et_setName, sp_selectDistrict (EditText, Spinner) there:

      btn_setData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Map<String, Object> userDataMap = new HashMap<String, Object>(); userDataMap.put("name", et_setName.getText().toString()); userDataMap.put("district", sp_selectDistrict.getSelectedItem().toString()); mFirestore.collection("user_data").document().set(userDataMap).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if(task.isSuccessful()){ Toast.makeText(SelectLocationActivity.this, "Запись прошла успешно", Toast.LENGTH_SHORT).show(); } else{ Toast.makeText(SelectLocationActivity.this, "Запись не удалась", Toast.LENGTH_SHORT).show(); } } }); } }); 

    Result: enter image description here