0

What I have is a calendar widget which opens up when pressed and the user can select their dates from the calendar. I have variables to save the dates entered in inside the DateSetup class and I followed the code on using putExtra() to send the data from one class to another, but the values are not showing up.

The process of the application is when the dates have been selected, the user will then be able to select the times with the time picker widget which works.

After the dates and times have been selected, the values are sent to a MySQL database which the dates/times values should be shown. Only the selected times are showing but not the dates from the calendar selected.

DateSetup class

public class DateSetup extends MainActivity implements View.OnClickListener
{

    //UI References
    private EditText fromDateEtxt;
    private EditText toDateEtxt;

    // Dates Strings setup
    String startDate;
    String endDate;


    DatePickerDialog fromDatePickerDialog;
    DatePickerDialog toDatePickerDialog;

    private SimpleDateFormat dateFormatter;

    private DataDBAdapter2 Database;

    Button btnHistory;
    Button btnTime;
    Button btnFuture;
    Button btnManual;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.date_setup);


        fromDateEtxt = (EditText) findViewById(R.id.etxt_fromdate);
        fromDateEtxt.setInputType(InputType.TYPE_NULL);
        fromDateEtxt.requestFocus();

        toDateEtxt = (EditText) findViewById(R.id.etxt_todate);
        toDateEtxt.setInputType(InputType.TYPE_NULL);
        toDateEtxt.requestFocus();


        btnTime = (Button)findViewById(R.id.btnSetDates);
        btnFuture = (Button)findViewById(R.id.btnFutureSetups);
        btnManual = (Button)findViewById(R.id.btnManualControl);


        fromDateEtxt.setOnClickListener(this);
        toDateEtxt.setOnClickListener(this);



        // Open the database
        Database = new DataDBAdapter2(this);
        Database.open();

        // Set the date format to English (Australia)
        dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.ENGLISH);

        btnHistory = (Button)findViewById(R.id.btnHistoryView);
        btnHistory.setOnClickListener(this);


        //-------------------------------------------
        // Start Date (From date)
        //-------------------------------------------
        Calendar newCalendar = Calendar.getInstance();
        fromDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener()
        {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                fromDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                //startDate = fromDateEtxt.getText().toString();

            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


        //-------------------------------------------
        // End Date (End date)
        //-------------------------------------------
        toDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener()
        {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
            {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                toDateEtxt.setText(dateFormatter.format(newDate.getTime()));
                //endDate = toDateEtxt.getText().toString();


            }

        },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


        // Set dates and continue to set times
        btnTime.setOnClickListener(new View.OnClickListener()//OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Start next activity
                Intent i = new Intent(DateSetup.this, TimeSetup.class);

                // Send data start data and End Date
                //i.putExtra("START", startDate);
                //i.putExtra("END", endDate);
                i.putExtra("START", fromDateEtxt.getText().toString());
                i.putExtra("END", toDateEtxt.getText().toString());

                //i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

                DateSetup.this.startActivity(i);
            }
        });


        // History view
        btnHistory.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v2)
            {
                Intent myIntent2 = new Intent(DateSetup.this, HistoryView.class);
                myIntent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                DateSetup.this.startActivity(myIntent2);
            }
        });


        // Future Settings View
        btnFuture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent myIntent3 = new Intent(DateSetup.this, CurrentView.class);
                myIntent3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                DateSetup.this.startActivity(myIntent3);

            }
        });


        // Manual Control
        btnManual.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent myIntent4 = new Intent(DateSetup.this, IrrigationControl.class);
                myIntent4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                DateSetup.this.startActivity(myIntent4);

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.date_setup, menu);
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    public void onClick(View view)
    {
        if(view == fromDateEtxt)
        {
            fromDatePickerDialog.show();
        }
        else if(view == toDateEtxt)
        {
            toDatePickerDialog.show();
        }
    }

}

TimeSetup2 class

public class TimeSetup2 extends TimeSetup
{
    Button btnBck;
    Button btnSetDatesandTime;

    String minutedisplay2;
    String hourdisplay2;

    private DataDBAdapter2 Database;

    private TimePicker timePicker2;

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

        btnBck = (Button) findViewById(R.id.btnBack);
        btnSetDatesandTime = (Button) findViewById(R.id.btnSet);
        timePicker2 = (TimePicker)findViewById(R.id.timePicker2);

        Database = new DataDBAdapter2(this);
        Database.open();

        // Back to previous activity
        // http://stackoverflow.com/questions/4038479/android-go-back-to-previous-activity
        btnBck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent myIntent = new Intent(TimeSetup2.this, TimeSetup.class);
                TimeSetup2.this.startActivity(myIntent);
            }
        });



        // Set time and dates
        btnSetDatesandTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                // Generate random number for ID
                Random r = new Random();
                int i2 = r.nextInt(100-1) + 100;

                // Convert from integer to string
                String random2 = Integer.toString(i2);


                //------------------------------------------------------
                // Data from other classes to be sent to the database
                //------------------------------------------------------
                Intent intent = getIntent();

                // Start Date and End date data
                String StartD = intent.getStringExtra("START");
                String EndD = intent.getStringExtra("END");

                // Time Picker 1 Data (Working)
                String StartTime1 = intent.getStringExtra("TIMEHOUR1");
                String EndTime1 = intent.getStringExtra("TIMEMIN1");

                // Time Picker 2 Data
                //String StartTime2 = intent.getStringExtra("TIMEHOUR2");
                //String EndTime2 = intent.getStringExtra("TIMEMIN2");
                hourdisplay2 = timePicker2.getCurrentHour().toString();
                minutedisplay2 = timePicker2.getCurrentMinute().toString();

                // Send data to the Database
                Database.insertData2(random2, StartD, EndD, StartTime1 + ":" + EndTime1, hourdisplay2 + ":" + minutedisplay2);

                // Send data to the arduino board

                // Show Message that the data has been sent
                Toast.makeText(getApplicationContext(), "Irrigation Setups have been sent", Toast.LENGTH_LONG).show();
            }
        });
    }

}

I've been stuck on trying to solve this problem for days. It's probably something really simple I'm missing out but I can't figure it out.

Sahil Bora
  • 173
  • 1
  • 12
  • You are putting START and END in the intent extras, but I don't see TIMEHOUR1 and TIMEMIN1, so you're likely to get null strings. – WLGfx Oct 11 '16 at 13:11

1 Answers1

0

The only data types that are OS-friendly are primitive types (int, long, float, boolean, etc...), so that means that putExtra() allows you to store primitives only. That's why you cant send a Calendar object.

Try to send a String of your period to your next activity.

Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28