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 MainActivity.java
- 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 My_DbHelper.java 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 (R2D_login.java) 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 r2d_login.java , 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.
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”.
0 comments:
Post a Comment