Академический Документы
Профессиональный Документы
Культура Документы
In the next window make sure the Create activity option is selected in
order to create a new activity for your project, and click Next. This is
optional as you can create a new activity after creating the project, but
you can do it all in one step.
You will be asked to specify some information about the new activity. In
the Layout Name text field you have to specify the name of the file that
will contain the layout description of your app. In our case the
file res/layout/main.xml will be created. Then, click Finish.
res/layout/main.xml
file :
<requestFocus />
</EditText>
<Button
android:id="@+id/addBtn"
19
20
21
22
23
24
25
26
27
28
29
30
31
32
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:onClick="addUser"
android:text="Add New" />
<Button
android:id="@+id/deleteBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/addBtn"
android:layout_below="@+id/editText1"
android:onClick="deleteFirstUser"
android:text="Delete First" />
33
34
<ListView
35
android:id="@android:id/list"
36
android:layout_width="match_parent"
37
android:layout_height="wrap_content"
38
android:layout_alignParentLeft="true"
39
android:layout_below="@+id/deleteBtn" >
40
</ListView>
41
42 </RelativeLayout>
3. Create a custom
SQLiteOpenHelper
Now we have to write the code of the application. First we have to create
a custom SQLiteOpenHelper. We have to create a new class for that. Use the
Package Explorer to navigate to the Package of the source files:
com.javacodegeeks.android.androidsqliteexample
and go
to New -> Class, fill out the form as in the picture below and press Finish:
10
11
12
13
14
15
16
17
21
22
23
24
25
26
27
28
29
30
31
36
37
38
39
40 }
06
07
08
09
10
11
12
13
14
return id;
15
16
17
18
19
return this.name;
20
21
22
23
24
25
26
27
28 }
return name;
}
5. Create a
StudentOperations
class.
Basically this wrapper will describe and implement the specific database
operations (e.g. add, delete) on a Student object.
StudentOperations.java:
01 package com.javacodegeeks.android.example.androidsqliteexample;
02
03 import java.util.ArrayList;
04 import java.util.List;
05
06 import android.content.ContentValues;
07 import android.content.Context;
08 import android.database.Cursor;
09 import android.database.SQLException;
10 import android.database.sqlite.SQLiteDatabase;
11
12 public class StudentOperations {
13
14
// Database fields
15
private DataBaseWrapper dbHelper;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
dbHelper.close();
31
32
33
34
35
36
values.put(DataBaseWrapper.STUDENT_NAME, name);
37
long studId =
database.insert(DataBaseWrapper.STUDENTS, null, values);
38
39
40
41
+ " = "
42
43
44
45
46
47
48
49
50
51
52
long id = comment.getId();
53
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 }
35
36
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 }