KEMBAR78
Datepicker | PDF | Android (Operating System) | Integer (Computer Science)
0% found this document useful (0 votes)
8 views2 pages

Datepicker

The document contains Java code for an Android application that includes a date and time picker, as well as a database helper class for managing student records. It allows users to insert and retrieve student information, including roll number, name, branch, marks, and percentage. The application utilizes SQLite for data storage and includes user interface components for input and output.

Uploaded by

moreganu53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Datepicker

The document contains Java code for an Android application that includes a date and time picker, as well as a database helper class for managing student records. It allows users to insert and retrieve student information, including roll number, name, branch, marks, and percentage. The application utilizes SQLite for data storage and includes user interface components for input and output.

Uploaded by

moreganu53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.example.datetimepicker; DBhelper.

java

import android.app.DatePickerDialog; package com.example.studentapp;

import android.app.TimePickerDialog; import android.content.Context;

import android.os.Bundle; import android.database.Cursor;

import android.widget.Button; import android.database.sqlite.*;

import android.widget.TextView; import android.content.ContentValues;

import androidx.appcompat.app.AppCompatActivity; public class DBHelper extends SQLiteOpenHelper {

import java.util.Calendar; public DBHelper(Context context) {

public class MainActivity extends AppCompatActivity { super(context, "StudentDB", null, 1);

Button btnDate, btnTime; }

TextView txtDate, txtTime; @Override

@Override protected void onCreate(Bundle public void onCreate(SQLiteDatabase db) {


savedInstanceState) {
db.execSQL("CREATE TABLE students(rollno INTEGER
super.onCreate(savedInstanceState); PRIMARY KEY, name TEXT, branch TEXT, marks INTEGER,
percentage REAL);");
setContentView(R.layout.activity_main);
}
btnDate = findViewById(R.id.btnDate);
@Override
btnTime = findViewById(R.id.btnTime);
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
txtDate = findViewById(R.id.txtDate); newVersion) {
txtTime = findViewById(R.id.txtTime); db.execSQL("DROP TABLE IF EXISTS students");

btnDate.setOnClickListener(v -> { onCreate(db);


Calendar c = Calendar.getInstance(); }

int year = c.get(Calendar.YEAR); public boolean insertStudent(int rollno, String name, String
branch, int marks, float percentage) {
int month = c.get(Calendar.MONTH);
SQLiteDatabase db = this.getWritableDatabase();
int day = c.get(Calendar.DAY_OF_MONTH);
ContentValues values = new ContentValues();
new DatePickerDialog(this, (view, y, m, d) -> {
values.put("rollno", rollno);
txtDate.setText(d + "/" + (m + 1) + "/" + y);
values.put("name", name);
}, year, month, day).show();
values.put("branch", branch);
});
values.put("marks", marks);
btnTime.setOnClickListener(v -> {
values.put("percentage", percentage);
Calendar c = Calendar.getInstance();
long result = db.insertWithOnConflict("students", null,
int hour = c.get(Calendar.HOUR_OF_DAY);
values, SQLiteDatabase.CONFLICT_REPLACE);
int minute = c.get(Calendar.MINUTE);
return result != -1;
new TimePickerDialog(this, (view, h, m) -> {
}
txtTime.setText(h + ":" + String.format("%02d", m));
public Cursor getStudent(int rollno) {
}, hour, minute, true).show();
SQLiteDatabase db = this.getReadableDatabase();
});
return db.rawQuery("SELECT * FROM students WHERE
} rollno = ?", new String[]{String.valueOf(rollno)});

} }}
package com.example.studentapp; Toast.makeText(this, "Insert Failed",
Toast.LENGTH_SHORT).show();
import android.database.Cursor;
}
import android.os.Bundle;
});
import android.widget.*;
btnSearch.setOnClickListener(v -> {
import androidx.appcompat.app.AppCompatActivity;
int roll = Integer.parseInt(etRollNo.getText().toString());
public class MainActivity extends AppCompatActivity {
Cursor c = dbHelper.getStudent(roll);
EditText etRollNo, etName, etBranch, etMarks, etPercentage;
if (c.moveToFirst()) {
Button btnInsert, btnSearch;
String data = "Name: " + c.getString(1) + "\n" +
TextView tvOutput;
"Branch: " + c.getString(2) + "\n" +
DBHelper dbHelper;
"Marks: " + c.getInt(3) + "\n" +
@Override
"Percentage: " + c.getFloat(4);
protected void onCreate(Bundle savedInstanceState) {
tvOutput.setText(data);
super.onCreate(savedInstanceState);
} else {
setContentView(R.layout.activity_main);
tvOutput.setText("Student not found!");
etRollNo = findViewById(R.id.etRollNo);
}
etName = findViewById(R.id.etName);
c.close();
etBranch = findViewById(R.id.etBranch);
});
etMarks = findViewById(R.id.etMarks);
}
etPercentage = findViewById(R.id.etPercentage);
void clearFields() {
btnInsert = findViewById(R.id.btnInsert);
etRollNo.setText("");
btnSearch = findViewById(R.id.btnSearch);
etName.setText("");
tvOutput = findViewById(R.id.tvOutput);
etBranch.setText("");
dbHelper = new DBHelper(this);
etMarks.setText("");

etPercentage.setText("");
btnInsert.setOnClickListener(v -> {
}
int roll = Integer.parseInt(etRollNo.getText().toString());
}
String name = etName.getText().toString();

String branch = etBranch.getText().toString();

int marks = Integer.parseInt(etMarks.getText().toString());

float percent =
Float.parseFloat(etPercentage.getText().toString());

boolean success = dbHelper.insertStudent(roll, name,


branch, marks, percent);

if (success) {

Toast.makeText(this, "Student Inserted",


Toast.LENGTH_SHORT).show();

clearFields();

} else {

You might also like