Android - 没有使用putExtra()

时间:2016-10-11 12:52:46

标签: java android mysql

我所拥有的是一个日历小部件,按下后会打开,用户可以从日历中选择日期。我有变量来保存在DateSetup类中输入的日期,我按照代码使用putExtra()将数据从一个类发送到另一个类,但是值没有显示出来。

应用程序的过程是在选择了日期后,用户将能够选择时间选择器小部件的时间。

在选择了日期和时间之后,将值发送到MySQL数据库,该数据库应显示日期/时间值。仅显示所选时间,但不显示所选日历中的日期。

DateSetup类

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类

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();
            }
        });
    }

}

我一直坚持试图解决这个问题好几天。它可能是非常简单的我错过了,但我无法弄明白。

1 个答案:

答案 0 :(得分:0)

唯一适合操作系统的数据类型是基本类型(int,long,float,boolean等),因此这意味着putExtra()允许您仅存储基元。这就是你无法发送Calendar对象的原因。

尝试将您期间的字符串发送到下一个活动。