如何使用datepicker和timepicker(android studio)在数据库中存储日期和时间?

7ivaypg9  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(454)

我正在尝试构建一个应用程序,其中用户选择日期和时间(通过datepicker和timepicker),然后按下一个名为“addexam”的按钮,该按钮将这些信息存储到sqlite数据库中。当用户单击“查看考试”时,日期和时间应该与数据库中存储的其他信息一起显示在屏幕上。
我在“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);
}

    }
piwo6bdm

piwo6bdm1#

你得把日期的一部分单独记下来

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

在这里看到更多

相关问题