1

I am developing an android application with firebase as my database. I want to insert some data on the press of a button, but some of the objects I want to insert in the database, is placed in different methods in my fragment. How can I insert data to my database across different methods using the same button?

public class mFragment extends Fragment implements 
DateFragment.DatePickerEvent {

DatabaseReference database;
FirebaseAuth mAuth;
EditText text1;
EditText text2;

@Override
public void onDateSelected(String date) {
    Button buttonDateText = (Button) getView().findViewById(R.id.buttonDate);
    buttonDateText.setText(date);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_frag, container, false);
    database = FirebaseDatabase.getInstance().getReference("Persons");
    mAuth = FirebaseAuth.getInstance();
    final FirebaseUser user = mAuth.getCurrentUser();


    Button pickDateButton = (Button) rootView.findViewById(R.id.buttonDate);
    Button submitButton = (Button) rootView.findViewById(R.id.buttonSubmit);
    text1= (EditText) rootView.findViewById(R.id.oddsView);
    text2= (EditText) rootView.findViewById(R.id.betAmountView);

    pickDateButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DateFragment picker = new DateFragment();
            picker.setDatePickerEvents(mFragment.this);
            picker.show(getFragmentManager(), "datePicker");
        }
    });

    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            database.child("Person").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String myText= text1.getText().toString().trim();
                    String myAnotherText= text2.getText().toString().trim();
                    String id = database.push().getKey();
                    database.child(user.getDisplayName()).child(id).child("Adress").setValue(myText);
                    database.child(user.getDisplayName()).child(id).child("name").setValue(myAnotherText);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
        }
    });
    return rootView;
}


}

The implemented interface comes from a datefragment I have made, where the user can choose a date on a date-widget. I want to insert the date object into the database, but I am unsure on how to do that with the submitButton being in another method

2
  • Give more clarity.. What exactly you want to achieve?!! Commented Aug 7, 2017 at 8:35
  • There is a submitButton which adds two textfields to database as a child of an unique id. In another method "onDateSelected" there is a date object, which i also want to add to the database as a child of the same unique id as the two textfields. All this should be done with the submitButton. Commented Aug 7, 2017 at 8:40

1 Answer 1

1

You already set the date to your Button using buttonDateText.setText(date); you can add this to your onDataChange and read it :

Button buttonDateText = (Button) getView().findViewById(R.id.buttonDate);
String date = buttonDateText.getText().toString();

or declare a String as class variable String mDate ="";, inside onDateSelected do mDate = date, now you can use mDate inside your onDataChange

Sign up to request clarification or add additional context in comments.

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.