Michael Smith
Michael Smith

Reputation: 29

Data is automatically disappearing when uploaded to Firebase

When sending the data to a Database in Firebase, for some reason the data is disappearing automatically. Here's the video showing: https://www.youtube.com/watch?v=0pWCNx7MsiY

I've been trying to solve this problem for several days, but it's not very difficult. Someone else might have the same problem.

Main code of MainActivity.class:

alreadyRegistered = findViewById(R.id.alreadyRegistered);
alreadyRegistered.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final String colorField = color.getText().toString();
        final String birthdateField = (birthdate.getText().toString());
        final String genderInString = genderField.getSelectedItem().toString();
        String identifierString = identifier.getText().toString().trim();
        String identifierStringInHash = encryptInMD5(identifierString);

        reference = FirebaseDatabase.getInstance().getReference().child("Users").child(identifierStringInHash);

        reference.addValueEventListener(new ValueEventListener() {
                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    reference = FirebaseDatabase.getInstance().getReference().child("Users").child(identifierStringInHash);

                        String name = "Testing";
                        String email = "[email protected]";
                        String telephone = "55555555555";

                        HashMap<String, String> sendDataToDataBase = new HashMap<>();
                        sendDataToDataBase.put("Name", name);
                        sendDataToDataBase.put("Email", email);
                        sendDataToDataBase.put("Identifier", identifierStringInHash);
                        sendDataToDataBase.put("Telephone", telephone);
                        sendDataToDataBase.put("Color1", colorField);
                        sendDataToDataBase.put("Birthdate", birthdateField);
                        sendDataToDataBase.put("Gender", genderInString);

                        try {
                            root .child(identifierStringInHash).setValue(sendDataToDataBase);
                            Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();

                        } catch (Exception e) {
                            e.getMessage();
                        }

                        Intent senderIntent = new Intent(getApplicationContext(), MainActivity2.class);
                        Bundle parameters = new Bundle();
                        parameters.putString("name", name);
                        parameters.putString("email", email);
                        parameters.putString("color1", colorField);
                        parameters.putString("birthdate", birthdateField);
                        parameters.putString("gender", genderInString);
                        parameters.putString("identifierStringInHash", identifierStringInHash);
                        parameters.putString("telephone", telephone);
                        senderIntent.putExtras(parameters);
                        startActivity(senderIntent);
                        Toast.makeText(getApplicationContext(), identifierStringInHash, Toast.LENGTH_LONG).show();
                    }
            });
        }
    });
}

MainActivity2.class code:

public class MainActivity2 extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    Button alreadyRegistered;
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference root = database.getReference().child("Users");
    DatabaseReference reference;
    ArrayList<String> arrayList_father;

    ArrayAdapter<String> arrayAdapter_father;

    ArrayList<String> arrayList_Brazil;
    ArrayList<String> arrayList_UnitedStates;
    ArrayList<String> arrayList_Germany;

    ArrayAdapter<String> arrayAdapter_child;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cadastrodeusuario2);

        Spinner spinner_color = findViewById(R.id.spinnerColor);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.color, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner_color.setAdapter(adapter);
        spinner_color.setOnItemSelectedListener(this);

        Spinner countryField = findViewById(R.id.spinner_country);

        Spinner city_spinner = findViewById(R.id.spinner_city);

        arrayList_father = new ArrayList<>();
        arrayList_father.add("Brazil");
        arrayList_father.add("United States");
        arrayList_father.add("Germany");

        arrayAdapter_father = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_father);

        countryField.setAdapter(arrayAdapter_father);

        arrayList_Brazil = new ArrayList<>();
        arrayList_Brazil.add("São Paulo");
        arrayList_Brazil.add("Rio de Janeiro");
        arrayList_Brazil.add("Brasília");

        arrayList_UnitedStates = new ArrayList<>();
        arrayList_UnitedStates.add("New York");
        arrayList_UnitedStates.add("San Francisco");
        arrayList_UnitedStates.add("Whashington");

        arrayList_Germany = new ArrayList<>();
        arrayList_Germany.add("Berlin");
        arrayList_Germany.add("Dortmund");
        arrayList_Germany.add("Stuttgart");

        countryField.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_Brazil);
                }
                if (position == 1) {
                    arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_UnitedStates);
                }
                if (position == 2) {
                    arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_Germany);
                }
                city_spinner.setAdapter((SpinnerAdapter) arrayAdapter_child);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        alreadyRegistered = findViewById(R.id.alreadyRegistered2);
        alreadyRegistered.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final String color2 = spinner_color.getSelectedItem().toString();
                final String country = countryField.getSelectedItem().toString();
                final String city = city_spinner.getSelectedItem().toString();

                Intent receiverIntent = getIntent();
                Bundle receiverBundle = receiverIntent.getExtras();
                String name = receiverBundle.getString("name");
                String email = receiverBundle.getString("email");
                String color1 = receiverBundle.getString("color1");
                String birthdate = receiverBundle.getString("birthdate");
                String gender = receiverBundle.getString("gender");
                String identifierStringInHash = receiverBundle.getString("identifierStringInHash");
                String telephone = receiverIntent.getStringExtra("telephone");

                reference = FirebaseDatabase.getInstance().getReference().child("Users");
                reference.addListenerForSingleValueEvent(new ValueEventListener() {

                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        HashMap<String, String> sendDataToDataBase = new HashMap<>();
                        sendDataToDataBase.put("Name", name);
                        sendDataToDataBase.put("Email", email);
                        sendDataToDataBase.put("Identifier", identifierStringInHash);
                        sendDataToDataBase.put("Telephone", telephone);
                        sendDataToDataBase.put("Color1", color1);
                        sendDataToDataBase.put("Birthdate", birthdate);
                        sendDataToDataBase.put("Gender", gender);
                        sendDataToDataBase.put("City", city);
                        sendDataToDataBase.put("Color2", color2);
                        sendDataToDataBase.put("Country", country);

                        try {
                            root.child(identifierStringInHash).setValue(sendDataToDataBase);
                            Toast.makeText(getApplicationContext(), country, Toast.LENGTH_LONG).show();

                        } catch (Exception e) {
                            e.getMessage();
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {
                    }
                });
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Spinner spinner_city = findViewById(R.id.spinner_city);
        if (position == 0)
            arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_Brazil);

        if (position == 1) {
            arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_UnitedStates);
        }
        if (position == 2) {
            arrayAdapter_child = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList_Germany);
        }

        spinner_city.setAdapter((SpinnerAdapter) arrayAdapter_child);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}

Please, anybody can help? I really can't fix this error myself. For some reason, the value sent to Firebase later becomes null. That's why the data in Firebase is disappearing. But why is the sent data turning into null?

Upvotes: 0

Views: 206

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The only place I see writing in your code is:

root.child(identifierStringInHash).setValue(sendDataToDataBase);

This writes the data in sendDataToDataBase to the identifierStringInHash path in the database, replacing any existing data at that path.


If you want to merge the data in sendDataToDataBase with the existing data in identifierStringInHash, you can call updateChildren():

root.child(identifierStringInHash).updateChildren(sendDataToDataBase);

With updateChildren any property that exists in sendDataToDataBase overwrites the existing value of that property under identifierStringInHash. But any new properties will be added to identifierStringInHash, and any properties that you don't specify will be left unmodified in identifierStringInHash.


If you instead want to keep a list of each submission, you'll want to cal push():

root.child(identifierStringInHash).push().setValue(sendDataToDataBase);

The push() call generates a new child node under identifierStringInHash and writes the data there. Each time you call push().setValue(...) it adds a new child node to the list.

Upvotes: 1

Related Questions