简体   繁体   中英

How to store date and time in the database using DatePicker and TimePicker (Android Studio)?

I am trying to build an app where the user selects date and time (through DatePicker and TimePicker), then presses a button called "Add Exam" which stores these information into a SQLite Database. When the user clicks on "View Exam", the date and time is supposed to show up on the screen along with other information stored in the database.

I get an error with the lines "date.getText().toString(), time1.getText().toString()" where "getText" is not being recognised.

 public class Exam extends AppCompatActivity {

DatabaseManager myDb;
EditText un, loc3;
DatePicker date;
TimePicker time1;

int year;
int monthOfYear;
int dayOfMonth;

Button btnAddExam;
Button btnViewExam;
Button btnDeleteExam;

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main5);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);
    myDb = new DatabaseManager(this);

    un = (EditText)findViewById(R.id.un);
    date = (DatePicker)findViewById(R.id.date);
    loc3 = (EditText)findViewById(R.id.loc3);
    time1 = (TimePicker) findViewById(R.id.time1);
    btnAddExam = (Button)findViewById(R.id.addexam);
    btnViewExam = (Button)findViewById(R.id.viewexam);
    btnDeleteExam = (Button)findViewById(R.id.deleteexam);

    AddExam();
    ViewExam();
    DeleteExam();

}


public  void AddExam() {
    btnAddExam.setOnClickListener(
            new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.O)
                @Override
                public void onClick(View v) {
                    boolean isInserted = myDb.insertDataExam(
                            un.getText().toString(),
                            loc3.getText().toString(),
                            date.getText().toString(),
                            time1.getText().toString()
                    );


                    if(isInserted == true)
                        Toast.makeText(Exam.this,"Data Inserted",Toast.LENGTH_LONG).show();
                    else
                        Toast.makeText(Exam.this,"Data not Inserted",Toast.LENGTH_LONG).show();
                }
            }
    );
}


public  void ViewExam() {
    btnViewExam.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {


                    Cursor res = myDb.getAllDataExam();
                    if(res.getCount() == 0) {

                        message("Error","Nothing found");
                        return;
                    }

                    StringBuffer buffer = new StringBuffer();

                    while (res.moveToNext()) {
                        buffer.append("Unit Name: "+ res.getString(0)+"\n\n");
                        buffer.append("Date: "+ res.getString(1)+"\n");
                        buffer.append("Time: "+ res.getString(2)+"\n");
                        buffer.append("Location: "+ res.getString(3)+"\n\n");

                    }

                    // Show all data
                    message("Exam Information",buffer.toString());


                }
            }
    );
}

public void DeleteExam() {
    btnDeleteExam.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            }
    );
}



public void message(String title,String Message){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(Message);
    builder.show();
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    if (id == R.id.home5) {

        Intent intent = new Intent(Exam.this, Home.class);
        startActivity(intent);
        return true;
    }



    return super.onOptionsItemSelected(item);
}

    }

You would have to get parts of the date individually

int day = datePicker.getDayOfMonth();
int month = datePicker.getMonth();
int year =  datePicker.getYear();

See more here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM