Sunteți pe pagina 1din 13

Android SQLite DATABASE TUTORIAL

1. Create a new Android Application with the name PRODUST IS.


2. Create two more activity with names, EditDeleteActivity and ViewActivity.
3. Open the activity_main.xml layout and design the following layout. Change the ID of all the
EditText, Radio Buttons, AutoCompleteTextView and Buttons to whatever you like that is related
to its function.

4. Open the activity_view.xml layout and design the following layout. Change the ID of the ListView
to whatever you like related to what it must contain

5. Open the activity_edit_delete.xml layout and design the following layout. Change the ID of all the
EditText, RadioButtons, AutoCompleteTextView and Buttons to whatever you like that is related to its
function. But be aware of the ID’s you set on earlier layouts. ID’s you are going to set to all form controls in
this layout should be different to others.
6. Close and save all changes to your layouts.
7. Go to the SRC directory, under the current package of your project; create a new class with the
name DataHelper.
8. Extend the DataHelper class to SQLiteOpenHelper class.

9. You can see that there is an error that says, you should implement the unimplemented methods
such as onCreate and onUpgrade. In order to import those methods, click on the light bulb in line
with the error and implement the said methods.

10. Once the methods are implemented, the codes for those methods will be automatically generated
similar to the one below but its functionality should be written by you.

11. You still have one error; that the constructor for your class is not yet implemented. In order for the
constructor to be implemented, you should again click on light bulb in line with the error and add
the constructor to implement it.
12. Once the constructor is implemented, the codes will be automatically generated similar to what is
illustrated below.

13. The error is already eliminated; the next thing you have to do is declare String variables that will
hold the names of your database, table and columns of your table. Those variables should be
declared in the global declaration section of you DataHelper class.

14. Modify the default parameters of your constructor that is generated automatically when your add
it to your class.

15. Supply the value of the different values of name, factory and version for your constructor in order
to create your database. Also, create an instance of the SQLiteDatabase inside your constructor.

16. Go to the onCreate method and modify the declared name of the SQLiteDatabase similar to the
instance you created for the SQLiteDatabase in the constructor.

17. Inside the onCreate method of your DataHelper Class, create a query that will create your table in
your database. You can see the code below.

18. Go to the onUpgrade method, modify the declared name of the SQLiteDatabase similar to what you
declares on the onCreate method.
19. Inside the onUpgrade method, create a query that deletes your table on your database.

20. Open your MainActivity.java class; in the global declaration section of the class, call the name of
the DataHelper class and create an instance of it. Cast the instance of that class inside the onCreate
method of MainActivity.

21. Once you’re done, save all your work and test your application in order to verify if the database
was created in your app. You can check it in the Dalvik Debug Monitor Server (DDMS).
22. After verifying that the database was created in your application, you can now start programming
the functions of each activity in your application.
a. MainActivity
i. Add/save record in the database
ii. Clear entered information in text fields
iii. Navigate the app to the ViewActivity
b. ViewActivity
i. Display the records in a listview
ii. Navigate the app to the EditDeleteActivity
c. EditDeleteActivity
i. Display the information selected from the listview of the ViewActivity
ii. Update information on the database
iii. Delete information on the database

Now let’s start coding for the MainActivity

23. On the global declaration section of the MainActivity, declare variables that will be used as
reference of the EditTexts, RadioGroup, RadioButtons, AutoCompleteTextViews and Buttons in
order to build its functionality.

24. Cast or call the declared names of each form elements on the onCreate method and refer to its
corresponding resource ID’s.
25. After casting all declared variable names, we should create a string-array that will be used by the
AutoCompleteTextView as suggestions to users what they probably want to type. So open the
strings.xml file and create a string array, save it with the name “arrayDealer”.

26. Once the string array is created, call it on the onCreate method and refer it to the
AutoCompleteTextView you have declared and set as an adapter. You have to insert the code after
the casting of your actvDealer.

27. Go to the DataHelper class; create a new method that will be used to insert record in the table in
the database.

28. Go back to MainActivity; inside the onCreated method, create a setOnClickListener method that
will be executed when the SAVE button is clicked. Do not forget to change the argument of onclikc
method inside the setOnclickListener.

29. Inside the onClick method of the setOnClickListener of the SAVE button; code the program flow to
save the record to the database.
30. After writing the codes for the SAVE button, next is for the CLEAR button. You may use either the
setOnClickListener or you can create a public method that will be called in the android:onClick
attribute of the Clear button in the xml file. This time I will be using the setOnClickListener method
inside the onCreate method of the current class, just after the code for the SAVE button.

31. Inside the onClick method of the setOnClickListener of the CLEAR button; code the program flow
that will clear all information entered on the text fields and selected on the radio buttons.

32. After writing the codes for the CLEAR button, the VIEW button shall follow. Using the
setOnClickListener, create a new method for the VIEW button.

33. Inside the onClick method of the setOnClickListener of the VIEW button; code the program flow
that will direct the application to the ViewActivity in order to see the list of products that were
saved into the database.

Now let’s start coding for the ViewActivity.

34. Open the ViewActivity class. In this class, includes the functionality of the ViewActivity activity to
display all saved product information in the database in a ListView that we dragged earlier on the
activity_view.xml layout.
35. On the global declaration section of the current class, call the DataHelper class and create an
instance of it. Cast the instance of that class inside the onCreate method of ViewActivity.
36. Go to the DataHelper class; create a new method that will be used to retrieve records in the table
in the database and will be displayed in the ListView.

37. Go back to the ViewActivity class, declare a variable that will serve as reference of the listview in
the activity_view.xml. Cast the declared variable inside the onCreate method and refer the ID of
the listview.

38. After casting the name of the listview on the onCreate method, write the codes that will populate
all product names from the database in the listview.

39. You can run the app in order to test that your codes are working properly.
40. After testing the app, the next to be done is to make all items in the list clickable where product
names saved into the database are listed. In order to do that you are going to create a
setOnItemClickListener for the listview inside the onCreate method below the codes added earlier.
41. As you can see, you have an error that says methods of the OnItemClickListener is not
implemented, so to implement the method, click the lightbulb inline with the error and select “Add
unimplemented method” similar to the one below.

42. Once the method is implemented, the following codes will be automatically generated. The error
will be also eliminated.

43. After implementing the method, change the name of its arguments for the AdapterView, View, int
and long to whatever you like.

44. Inside the onItemClick method, write the codes that will enable the app to send information form
the ViewActivity(which is the current activity) to the EditDeleteActivity when one of the item on
the listview is clicked. First thing to do is, when the item is clicked by the user we should get the
name of the product that is on the item and save it in a String variable.

45. Once the product name is saved in a variable, the next thing to do is to get the ID of that product.
Wherein the ID is stored in the table in the database. So we need a method that will retrieve the ID
of a selected product. To do that, go the DataHelper class and create a method.

46. After creating the method above, we can now invoke or call the method to retrieve the ID of the
selected product inside the onItemClick method of the listview in ViewActivity.java class.
After doing so, we are now ready to start coding for the EditDeleteActivity.

47. What are we going to the in the EditDeleteActivity is, we are going to receive the data (containing
the ID and the NAME of the product) sent from the ViewActivity. But the first thing to do is to cast
every form elements in layout connected to class, so we can display the data properly. Now open
EditDeleteActivity.java class, and do the math.

48. Once every form elements is casted, do not forget to call the string array as an adapter of the
autocompletetextview for its suggestions to user input.

49. Next thing to do, is to receive the data (which includes the ID and the NAME of the selected
product) sent using an intent from the ViewActivity to the EditDeleteActivity. To do that, we first
need to declare two variables that will store the values of the intent that has been sent. Go to the
global declaration section of the current class and decalare one integer variable (for the ID) and
one String variable (for the NAME).
50. After that, create an intent just below the casting of variables to each form elements that will
receive the value sent from the other activity and store the values on the variables declared
earlier.

51. Once the values are stored in the variables we are going to use them in order to retrieve all
product information of the selected product in a query statement. So go to the DataHelper class
and create a new method. Go ahead and make one.

52. After creating the method, we can now invoke or call that method, save its result into another
cursor variable inside the onCreate method of the EditDeleteActivity class and extract its
individual values to be displayed in the texfields, and radiobuttons.

Code for displaying data retrieved


from the database to a radio button.

It is just a conditional statement.

53. You can test the application order to see whether the codes that were written are functioning well
and correct.
54. If you have seen that information of specific products selected are displayed correctly, we can
now move on writing the codes to the update the displayed information of a specific product.
55. Go to the DataHelper class and create a method that when called will update all the information of
a product in the database.

56. Now, we are going to write the codes that will be executed when the UPDATE button was clicked.
To do that we are going to use an OnClickListener method inside the onCreate method of the
EditDeleteActivity so that the button can respond to a click event.
57. Inside the onClick method for the UPDATE button write the codes that will update the product
information.

58. Test your application if the codes are working properly.


59. After coding the functionality for the UPDATE button, next up is for the DELETE button. We need a
new method to execute our Delete query. So go to the DataHelper class and create a new one.

60. Once the method is created, we can now create an onClickListener for the DELETE button.
61. Inside the onClick method of the OnClickListener of the DELETE button, write the code that will
delete the product record in the database.

62. Those were the codes for the DELETE button, basically it became too long using the AlertDialog in
order to deliver message to the users.
63. Now, the program is ready. But we need few for changes for the navigations of each activity to
another.
64. We need to make the HOME icon in the action bar of each activity to be used as a BACK button. We
need enable the home icon by inserting the following codes in every onCreate method of each
class where you want to have a back button.

65. After inserting that, the icon can only be clicked but it has no functions yet. In order for that home
icon to work as a back button, we are going to create the following method inside each class.
66. You can copy paste the above method to other classes, but you need to change the declared class
to go back to the proper activity.
67. In every codes that, that were written above, when directed to a new activity, the current activity
is closed, so when you press the back button of the device, the application will automatically
closed. In order to trap that error, we need to add a code for the back button when pressed using
the onBackPressed method as written below.

68. If you are going to implement the method above on other activity, you just need to change the
class that is called when the intent starts.

S-ar putea să vă placă și