· 9 years ago · Oct 03, 2016, 12:06 AM
1package au.edu.rmit.contact;
2
3import android.support.v7.app.AppCompatActivity;
4import android.os.Bundle;
5import android.os.Bundle;
6import android.view.View;
7import android.widget.EditText;
8import android.widget.TextView;
9import android.widget.Toast;
10import android.app.Activity;
11import android.database.Cursor;
12import android.database.sqlite.SQLiteDatabase;
13
14public class MainActivity extends Activity {
15SQLiteDatabase db;
16TextView tv;
17EditText et1,et2,et3,query;
18
19@Override
20protected void onCreate(Bundle savedInstanceState) {
21 super.onCreate(savedInstanceState);
22 setContentView(R.layout.activity_main);
23 //initialize all view objects
24 tv=(TextView)findViewById(R.id.textView1);
25 et1=(EditText)findViewById(R.id.editText1);
26 et2=(EditText)findViewById(R.id.editText2);
27 et3=(EditText)findViewById(R.id.editText);
28
29 //create database if not already exist
30 db= openOrCreateDatabase("Mydb2", MODE_PRIVATE, null);
31 //create new table if not already exist
32 db.execSQL("create table if not exists roster1(name varchar, sur_name varchar, time varchar)");
33}
34//This method will call on when we click on insert button
35public void insert(View v)
36{
37 String name=et1.getText().toString();
38 String sur_name=et2.getText().toString();
39 String time=et3.getText().toString();
40 et1.setText("");
41 et2.setText("");
42 et3.setText("");
43 //insert data into able
44 db.execSQL("insert into roster1 values('"+name+"','"+sur_name+"','"+time+"')");
45 //display Toast
46 Toast.makeText(this, "values inserted successfully.", Toast.LENGTH_LONG).show();
47}
48//This method will call when we click on display button
49public void display(View v)
50{
51 //use cursor to keep all data
52 //cursor can keep data of any data type
53 Cursor c=db.rawQuery("select * from roster1", null);
54 tv.setText("");
55 //move cursor to first position
56 c.moveToFirst();
57 //fetch all data one by one
58 do
59 {
60 //we can use c.getString(0) here
61 //or we can get data using column index
62 String name=c.getString(c.getColumnIndex("name"));
63 String surname=c.getString(1);
64 String time=c.getString(2);
65
66 //display on text view
67 tv.append("Name:"+name+" , Roles:"+surname+", Time:"+time+"n");
68 //move next position until end of the data
69 }while(c.moveToNext());
70}
71 //------------------Search------------------------//
72public void find(View v)
73{
74 query=(EditText)findViewById(R.id.editText4);
75
76 String search=query.getText().toString();
77 query.setText("");
78
79 //use cursor to keep all data
80 //cursor can keep data of any data type
81
82 Cursor c=db.rawQuery("select * from roster1 where Name = ?",new String[] {"%" + search + "%"});
83
84
85 tv.setText("");
86 //move cursor to first position
87 c.moveToFirst();
88 //fetch all data one by one
89 do
90 {
91 //we can use c.getString(0) here
92 //or we can get data using column index
93 String name=c.getString(c.getColumnIndex("name"));
94 String surname=c.getString(1);
95 String time=c.getString(2);
96
97 //display on text view
98 tv.append("Name:"+name+" , Roles:"+surname+", Time:"+time+"n");
99 //move next position until end of the data
100 }while(c.moveToNext());
101}
102
103
104
105}