0

hi people i have a hotel booking form which has got two datepickers and three spinners to collect the user data(checkin, checkout, rooms, adults,children)... i have tried to save the form data to my firebase, the code is working fine but i get really weird data on my database , i must have missed something on the string conversion.. here are the records in the database

adults: "android.support.v7.widget.AppCompatSpinner{e09c..."

checkin: "android.widget.DatePicker{14eadf8e V.E..... ......"

checkout: "android.widget.DatePicker{194a02de V.E..... ......"

children: "android.support.v7.widget.AppCompatSpinner{1dba..."

rooms: "android.support.v7.widget.AppCompatSpinner{d413..."

my BookingActivity is as follows

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_booking_form);
        Firebase.setAndroidContext(this);
        reserve = (Button) findViewById(R.id.reserve);
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        spinner3 = (Spinner) findViewById(R.id.spinner3);
        datePicker0 = (DatePicker) findViewById(R.id.datePicker0);
        datePicker2 = (DatePicker) findViewById(R.id.datePicker2);






        reserve.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating firebase object
                final Firebase ref = new Firebase("https://ngong-hills.firebaseio.com/");

                //Getting values to store

                String Checkin = datePicker0.toString().trim();
                String Checkout = datePicker2.toString().trim();
                String rooms = spinner1.toString().trim();
                String adults = spinner2.toString().trim();
                String children = spinner3.toString().trim();

                Booking booking = new Booking();
                booking.setadults(adults);
                booking.setchildren(children);
                booking.setrooms(rooms);
                booking.setCheckout(Checkout);
                booking.setCheckin(Checkin);

                Firebase newRef = ref.child("Bookings").push();
                newRef.setValue(booking);


            }
        });

    }

    @Override
    public void onClick(View view) {

        if(view == reserve){
            //open login activity when user taps on the already registered textview
            startActivity(new Intent(this, HomeNav.class));
        }

    }

i dont know what i missed ..

Sir George
  • 423
  • 3
  • 8
  • 20

1 Answers1

1
String Checkin = datePicker0.toString().trim();
String Checkout = datePicker2.toString().trim();
String rooms = spinner1.toString().trim();
String adults = spinner2.toString().trim();
String children = spinner3.toString().trim();

You're actually getting the the whole view element's(Spinner, DatePicker) string representation through toString method. Rather trying to do that, get the Spinner value like this,

String rooms = spinner1.getSelectedItem().toString();

Do the same for rest of the Spinners. Here is how you can get the date values from DatePicker and convert it to String.

Community
  • 1
  • 1
fluffyBatman
  • 6,524
  • 3
  • 24
  • 25
  • thanks so much @fluffyBatman the spinners worked correctly, but i am having trouble with the datepicker if i try to use the code on the page you have refered me to, my program crashes, can you just write me your own code please – Sir George Dec 14 '16 at 14:21
  • this is my logcat Caused by: java.lang.ClassNotFoundException: Didn't find class "android.icu.text.SimpleDateFormat" on path: DexPathList[[zip file "/data/app/com.example.moses.ngongapp-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] – Sir George Dec 14 '16 at 14:38
  • never mind i fixed it......only that the date value sent to the database is not true or real per say..when i enter 26th dec 2016 i get 9/6/19 00:00" its kind of wierd – Sir George Dec 14 '16 at 15:11
  • You should import `java.text.SimpleDateFormat` instead of `android.icu.text.SimpleDateFormat`. That should do the job regarding of the error you were having. As for the latter, what is the format String you put in SimpleDateFormat's constructor? "MM-dd-yyyy" or somethign else? – fluffyBatman Dec 14 '16 at 17:17
  • i am using the same one you have written "MM-dd-yyyy" , i dont know why i am getting an invalid date records – Sir George Dec 15 '16 at 08:27