简体   繁体   English

如何使用DatePicker和TimePicker(Android Studio)在数据库中存储日期和时间?

[英]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. 我正在尝试构建一个应用程序,用户可以在其中选择日期和时间(通过DatePicker和TimePicker),然后按下一个名为“添加考试”的按钮,该按钮将这些信息存储到SQLite数据库中。 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. 我在“ date.getText()。toString(),time1.getText()。toString()”行中收到错误,其中“ getText”未被识别。

 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 在这里查看更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想将使用日期选择器对话框和时间选择器对话框获得的日期和时间传递给 Android 工作室中的下一个活动 - I want to pass Date and Time I got using Datepicker Dialog and Timepicker Dialog to next Activity in Android studio Android Studio TimePicker和DatePicker - Android studio TimePicker and DatePicker 如何在Android的SQLite数据库中插入日期选择器和时间选择器 - How to insert datepicker and timepicker in sqlite database in android Android Studio DatePicker / TimePicker渲染/ nullpointerexception问题? - Android Studio DatePicker/TimePicker rendering/nullpointerexception problems? 如何在Android Studio上使用TimePicker在指定时间在Android中安排通知 - how to Scheduled Notifications in Android on a specified time with TimePicker on android studio 如何将日期选择器中的日期存储到mysql数据库中? - How to store date from datepicker into mysql database? 如何在 Android 中为 DatePicker 和 TimePicker 设置限制 - How to set restrictions for DatePicker and TimePicker in Android 如何在 Android Studio 3 中制作 timePicker - How to make timePicker in Android Studio 3 如何使用TimePicker获取今天的日期/时间? - How to get the today's date / time using the TimePicker? 如何将从日期选择器中选取的每个日期存储到Android上的内存或数据库中? - How can I store each date picked from datepicker into memory or database on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM