Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.database;
- import androidx.appcompat.app.AppCompatActivity;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class Database2 extends AppCompatActivity {
- Button back;
- Button next;
- Button homeb;
- TextView text1;
- TextView text2;
- SQLiteDatabase db;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_database2);
- text1 = (TextView) findViewById(R.id.tname);
- text2 = (TextView) findViewById(R.id.cname);
- back = (Button) findViewById(R.id.bb1);
- next = (Button) findViewById(R.id.bb2);
- homeb = (Button) findViewById(R.id.bb3);
- db = openOrCreateDatabase("Mydb",MODE_PRIVATE,null);
- final Cursor c = db.rawQuery("select * from student",null);
- c.moveToFirst();
- text1.setText(c.getString(c.getColumnIndex("name")));
- text2.setText(c.getString(c.getColumnIndex("college")));
- next.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- c.moveToNext();
- text1.setText(c.getString(c.getColumnIndex("name")));
- text2.setText(c.getString(c.getColumnIndex("college")));
- }catch (Exception e){
- Toast.makeText(getApplicationContext(),"Last Record",Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
- });
- back.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- c.moveToPrevious();
- text1.setText(c.getString(c.getColumnIndex("name")));
- text2.setText(c.getString(c.getColumnIndex("college")));
- }catch (Exception e) {
- Toast.makeText(getApplicationContext(),"First Record",Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
- });
- homeb.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent a = new Intent(Database2.this, MainActivity.class);
- startActivity(a);
- finish();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement