0

I am developing a Matrimony, I have multiple pages to store data that's why I have used SharedPreferences. I have pages like Basic Details,Social Details,Career Details,Login Details and on Login Details I want to retrieve the stored data from SharedPreferences and send that data to firebase realtime database.How can I do that?

This is the snippet for database:

This is the snippet for the child nodes:

I have tried to pass the SharedPreferences object "sp" but because of it a new node is generated i.e. "all" Below is the code of Login details

public class LoginDetails extends AppCompatActivity {
    EditText etemail_id,etpassword;
    TextView tvlogin_details;
    Button btregister,btback;
    FirebaseAuth mAuth;
    FirebaseDatabase database;
    DatabaseReference reference;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_details);
        etemail_id = (EditText) findViewById(R.id.etEmailid);
        etpassword = (EditText) findViewById(R.id.etPassword);

        tvlogin_details = (TextView) findViewById(R.id.tvLoginDetails);
        btback = (Button) findViewById(R.id.btBack);
        mAuth = FirebaseAuth.getInstance();
        btregister = (Button) findViewById(R.id.btRegister);
        database=FirebaseDatabase.getInstance();
        reference=database.getReference();
        //users= String.valueOf(new Users());

        btregister.setOnClickListener(new View.OnClickListener() {

            private final String PREFERENCE_FILE_KEY = "myAppPreference";

            @Override
            public void onClick(View view) {
                final String email = etemail_id.getText().toString().trim();
                final String password = etpassword.getText().toString().trim();

                if (TextUtils.isEmpty(email)) {
                    etemail_id.setError("Provide your Email first!");
                    etemail_id.requestFocus();
                }
                else if (TextUtils.isEmpty(password)) {
                    etpassword.setError("Enter Password!");
                    etpassword.requestFocus();
                }
                else if (!(email.isEmpty() && password.isEmpty())) {
                    mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(LoginDetails.this, new OnCompleteListener<AuthResult>() {
                        @Override
                        public void onComplete(@NonNull Task<AuthResult> task) {
                            if (task.isSuccessful()) {

                                reference.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                       // SharedPreferences sharedPreferences=this.getActivity().getSharedPreferences(PREFERENCE_FILE_KEY,Context.MODE_PRIVATE);
                                        SharedPreferences sp = getSharedPreferences("Mypref", 0);
                                        String first_name=sp.getString("first_name", null);
                                        String last_name=sp.getString("last_name",null );
                                        String fathers_name=sp.getString("fathers_name", null);
                                        String date=sp.getString("date", null);
                                        String income=sp.getString("income", null);
                                        String phone_no=sp.getString("phone_no", null);
                                        String occupation1=sp.getString("occupation1", null);
                                        String occupation=sp.getString("occupation", null);
                                        String city_state=sp.getString("city_state", null);
                                        String gender=sp.getString("gender", null);
                                        String highest_education1=sp.getString("highest_education1", null);
                                        String highest_education=sp.getString("highest_education", null);
                                        String marital_status1=sp.getString("marital_status1", null);
                                        String fathers_occupation=sp.getString("fathers_occupation", null);
                                        String mothers_occupation=sp.getString("mothers_occupation", null);
                                        String family_members=sp.getString("family_members", null);
                                        String height=sp.getString("height", null);
                                        String income1=sp.getString("income1", "");
                                        String city_state1=sp.getString("city_state1", null);
                                        String marital_status=sp.getString("marital_status", null);
                                        String mothers_name=sp.getString("mothers_name", null);
                                        String height1=sp.getString("height1", null);
                                        String hobbies=sp.getString("hobbies", null);
                                        String user_id=sp.getString("user_id", null);
                                        String siblings_names=sp.getString("siblings_names", null);
                                        String age=sp.getString("age", null);


                                        reference.child("User").child(Objects.requireNonNull(mAuth.getUid())).setValue(sp);
                                        Toast.makeText(getApplicationContext(), "data entered", Toast.LENGTH_SHORT).show();
                                    }
                                    @Override
                                    public void onCancelled(@NonNull DatabaseError databaseError) {

                                        Toast.makeText(getApplicationContext(), "database error", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                Toast.makeText(LoginDetails.this, "Successfully registered", LENGTH_LONG).show();
                                Intent it = new Intent(getApplicationContext(), Login.class);
                                startActivity(it);
                            }

                            else {
                                Toast.makeText(LoginDetails.this, "Registration Error", LENGTH_LONG).show();
                            }
                        }
                    });
                }
                else {
                    Toast.makeText(LoginDetails.this, "Error", Toast.LENGTH_SHORT).show();
                }
             }
        });

        btback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it1 = new Intent(getApplicationContext(), Preferences.class);
                startActivity(it1);
            }
        });
    }
}

I expected the result to be User->authkey->mydata.

The output was User->authkey->all->mydata

Joker
  • 796
  • 1
  • 8
  • 24

1 Answers1

0

You might be encountering this issue because the of the structure of the SharedPreferences file.

Try arranging your info in a new HashMap that has all the info you need in it as key : value and than try sending the new HashMap as the value to FireBase

Example :

HashMap hashMap = new HashMap();

SharedPreferences sp = getSharedPreferences("Mypref", 0);
hashMap.put("first_name", sp.getString("first_name", null))
hashMap.put("last_name", sp.getString("last_name",null))
hashMap.put("fathers_name", sp.getString("fathers_name", null))
hashMap.put("date", sp.getString("date", null))
hashMap.put("income", sp.getString("income", null))
(.......)

reference.child("User").child(Objects.requireNonNull(mAuth.getUid())).setValue(hashMap);

If that is not the case, you'll most probably need to iterate on the list of details you have and set one at a time as a value under your user's UID .

Example:

Use the HashMap we built in the first attempt :

Iterator iterator = hashMap.entrySet().iterator() 
if (hashMap.hasNext()) {
    iterator.next() 
    reference.child("User").child(Objects.requireNonNull(mAuth.getUid())).child(iterator.getKey).setValue(iterator.getValue);
}
Itay Feldman
  • 846
  • 10
  • 23