Advertisement
Shishu

JAVA 2nd file for android database

Dec 13th, 2019
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package com.example.database;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.content.Intent;
  6. import android.database.Cursor;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12. import android.widget.Toast;
  13.  
  14. public class Database2 extends AppCompatActivity {
  15.  
  16.     Button back;
  17.     Button next;
  18.     Button homeb;
  19.     TextView text1;
  20.     TextView text2;
  21.     SQLiteDatabase db;
  22.  
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_database2);
  27.  
  28.         text1 = (TextView) findViewById(R.id.tname);
  29.         text2 = (TextView) findViewById(R.id.cname);
  30.         back = (Button) findViewById(R.id.bb1);
  31.         next = (Button) findViewById(R.id.bb2);
  32.         homeb = (Button) findViewById(R.id.bb3);
  33.  
  34.         db = openOrCreateDatabase("Mydb",MODE_PRIVATE,null);
  35.         final Cursor c = db.rawQuery("select * from student",null);
  36.         c.moveToFirst();
  37.  
  38.         text1.setText(c.getString(c.getColumnIndex("name")));
  39.         text2.setText(c.getString(c.getColumnIndex("college")));
  40.  
  41.         next.setOnClickListener(new View.OnClickListener() {
  42.             @Override
  43.             public void onClick(View view) {
  44.                 try {
  45.                     c.moveToNext();
  46.                     text1.setText(c.getString(c.getColumnIndex("name")));
  47.                     text2.setText(c.getString(c.getColumnIndex("college")));
  48.                 }catch (Exception e){
  49.                     Toast.makeText(getApplicationContext(),"Last Record",Toast.LENGTH_LONG).show();
  50.                     e.printStackTrace();
  51.                 }
  52.  
  53.             }
  54.         });
  55.  
  56.         back.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View view) {
  59.                 try {
  60.                     c.moveToPrevious();
  61.                     text1.setText(c.getString(c.getColumnIndex("name")));
  62.                     text2.setText(c.getString(c.getColumnIndex("college")));
  63.                 }catch (Exception e) {
  64.                     Toast.makeText(getApplicationContext(),"First Record",Toast.LENGTH_LONG).show();
  65.                     e.printStackTrace();
  66.                 }
  67.  
  68.             }
  69.  
  70.         });
  71.  
  72.  
  73.         homeb.setOnClickListener(new View.OnClickListener() {
  74.             @Override
  75.             public void onClick(View v) {
  76.                 Intent a = new Intent(Database2.this, MainActivity.class);
  77.                 startActivity(a);
  78.                 finish();
  79.             }
  80.         });
  81.  
  82.  
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement