Android Database Part II

Hello World! Welcome back to Android Database series to learn some basic database features of the SQLite in Android. This post builds on the concepts and classes made in the previous part , i.e, Android Database Part I. So I would recommend you to go through it before reading this one.

Now I would assume that you have read the previous post on database & already have the classes and activities implemented there, cause we are gonna add to them in this part. So let's begin:

1. Changes in

  • There are as such no big changes in this main activity. I only removed all the code from the onClickListner of sign in button , and created a new function to increase readability.

2. Changes in class

  • We created two new methods here in this class , one to delete selected users from the database , & other to update any selected user info from the list.
  • In the delete-user() method we just get an object of SQLiteDatabase for our class in the writable mode using getWritableDatabase(). Then we pass the id value passed to the method, to the .delete() method.
  • In the same way we create update_user() method and create a ContentValues object using the parameters passed. The .update() method is then used.

3. Changes in Admin login ( activity

  • In this layout of this activity we add two new buttons to the activity_r2d_login.xml file.They are named Update & Delete.
  • We bind the variable created in , to the xml elements using findViewById() method. The methods under the corresponding onClickListner s & other are explained at the end in the last section.
  • For now just copy & paste the code if you want to. 

4. Create a new activity named Update_form

  • This activity uses the layout of our MainActivity so we don't need to create  a separate xml layout for this. If one is created, just delete it.
  • We re-use the layout by just setting it in the setContentView() method. We also change the text displayed on the button using the setText(" string ") method on the button object.
  • In the onClickListner() of the button, we call the same function used in the MainActivity to for the basic form verification.The only difference here is instead of inserting into the table we update the selected (selected in the r2d_login activity )user' s info.
  • To help us do this we used the intuitive method on putting extras into the intents. We put the id of the user when selected in r2d_login activity & put it in the intent using .putExtra("name",value) method.
  • We get the extra value back using the getExtras().get("id") method. Then we simply call the update_user method with mail_id, pass & id from intent. 
  • Finally we close the activity using finish() method.

5. Run the project

  • Simply run the app using CTRL+F11.

Admin login id
Users in database

Try select more than one user
Get a notification

Select a user to update
Enter new details & Update

Updated list
Select users to delete

List after deletion

Code Explained:

  1. The methods update_user and delete_user are quite straight forward. They are explained in point 2 above.
  2. The main magic happens in the r2d_login activity. 
  3. The overridden onResume() method calls the update_list() method in which we just create a new arrayadapater and use show_in_list() method (as in previous post) to initialize the list. We then set the grid view with the array adapter.
  4. Both the onClickListner s of the buttons use their own logics by calling remove_logic() & update_logic() respectively. They also call the same update_list() method to make updation happen.
  5. In the remove_logic() method we first create a SparseBooleanArray and set to checked grid positions as before. We then loop through the arrayadapater and check if the item is checked. If its checked we get its id by getting  the characters from the list data up to the first space (' '). This is done using the substring() method.We then call the delete_user(user_id) method.
  6. The update logic is more or less the same. The only difference is that we first check if more than one list item is checked & if so we prompt the user.
  7. Then using the same looping logic we find the checked list item id and put it int the extras field and start the activity.

Download Code:


Shobhit Chittora

Hi there, I am Shobhit Chittora. I am a college student and like to develop apps for Android platform.I started this blog to share my experiences with Android development and may be help you guys on the “roadtodroid”.

  • Image
  • Image
    Blogger Comment
    Facebook Comment


Post a Comment