SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c
Database - Package
The main package is android.database.sqlite that contains the classes to manage your own databases
Database - Creation
In order to create a database you just need to call this method openOrCreateDatabase with your database name and mode as a parameter. It returns an instance of SQLite database which you have to receive in your own object.Its syntax is given below
Apart from this , there are other functions available in the database package , that does this job. They are listed below
Sr.No | Method & Description |
---|---|
1 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags, DatabaseErrorHandler errorHandler) This method only opens the existing database with the appropriate flag mode. The common flags mode could be OPEN_READWRITE OPEN_READONLY |
2 | openDatabase(String path, SQLiteDatabase.CursorFactory factory, int flags) It is similar to the above method as it also opens the existing database but it does not define any handler to handle the errors of databases Marshall jmd 1 problem. |
3 | openOrCreateDatabase(String path, SQLiteDatabase.CursorFactory factory) It not only opens but create the database if it not exists. This method is equivalent to openDatabase method. |
4 | openOrCreateDatabase(File file, SQLiteDatabase.CursorFactory factory) This method is similar to above method but it takes the File object as a path rather then a string. It is equivalent to file.getPath() |
Database - Insertion
we can create table or insert data into table using execSQL method defined in SQLiteDatabase class. Its syntax is given below
This will insert some values into our table in our database. Another method that also does the same job but take some additional parameter is given below
Sr.No | Method & Description |
---|---|
1 | execSQL(String sql, Object[] bindArgs) This method not only insert data , but also used to update or modify already existing data in database using bind arguments |
Database - Fetching
We can retrieve anything from database using an object of the Cursor class. We will call a method of this class called rawQuery and it will return a resultset with the cursor pointing to the table. We can move the cursor forward and retrieve the data.
There are other functions available in the Cursor class that allows us to effectively retrieve the data. That includes
Sr.No | Method & Description |
---|---|
1 | getColumnCount() This method return the total number of columns of the table. |
2 | getColumnIndex(String columnName) This method returns the index number of a column by specifying the name of the column |
3 | getColumnName(int columnIndex) This method returns the name of the column by specifying the index of the column |
4 | getColumnNames() This method returns the array of all the column names of the table. |
5 | getCount() This method returns the total number of rows in the cursor |
6 | getPosition() This method returns the current position of the cursor in the table |
7 | isClosed() This method returns true if the cursor is closed and return false otherwise |
Android Context Class
Database - Helper class
For managing all the operations related to the database , an helper class has been given and is called SQLiteOpenHelper. It automatically manages the creation and update of the database. Its syntax is given below
Example
Here is an example demonstrating the use of SQLite Database. It creates a basic contacts applications that allows insertion, deletion and modification of contacts.
To experiment with this example, you need to run this on an actual device on which camera is supported.
Steps | Description |
---|---|
1 | You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication. |
2 | Modify src/MainActivity.java file to get references of all the XML components and populate the contacts on listView. |
3 | Create new src/DBHelper.java that will manage the database work |
4 | Create a new Activity as DisplayContact.java that will display the contact on the screen |
5 | Modify the res/layout/activity_main to add respective XML components |
6 | Modify the res/layout/activity_display_contact.xml to add respective XML components |
7 | Modify the res/values/string.xml to add necessary string components |
8 | Modify the res/menu/display_contact.xml to add necessary menu components |
9 | Create a new menu as res/menu/mainmenu.xml to add the insert contact option |
10 | Run the application and choose a running android device and install the application on it and verify the results. |
Following is the content of the modified MainActivity.java.
How to make sims 4 custom content hair. Following is the modified content of display contact activity DisplayContact.java
Following is the content of Database class DBHelper.java
Following is the content of the res/layout/activity_main.xml
Following is the content of the res/layout/activity_display_contact.xml
Following is the content of the res/value/string.xml
Following is the content of the res/menu/main_menu.xml
Following is the content of the res/menu/display_contact.xml
This is the defualt AndroidManifest.xml of this project
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from Android studio , open one of your project's activity files and click Run icon from the tool bar. Before starting your application,Android studio will display following window to select an option where you want to run your Android application.
Select your mobile device as an option and then check your mobile device which will display following screen −
Now open your optional menu, it will show as below image: Optional menu appears different places on different versions
Click on the add button of the menu screen to add a new contact. It will display the following screen −
It will display the following fields. Please enter the required information and click on save contact. It will bring you back to main screen.
Now our contact sai has been added.In order to see that where is your database is created. Open your android studio, connect your mobile. Go tools/android/android device monitor. Now browse the file explorer tab. Now browse this folder /data/data/<your.package.name>/databases<database-name>.
In an Android application, how do you start a new activity (GUI) when a button in another activity is clicked, and how do you pass data between these two activities?
MiguelHincapieC3,34222 gold badges2929 silver badges5858 bronze badges
AdhamAdham25k8989 gold badges206206 silver badges323323 bronze badges
22 Answers
Easy.
Extras are retrieved on the other side via:
Don't forget to add your new activity in the AndroidManifest.xml:
DenisKolodin4,60011 gold badge3434 silver badges4141 bronze badges
EmmanuelEmmanuel14.8k44 gold badges3737 silver badges6464 bronze badges
Create an intent to a ViewPerson activity and pass the PersonID (for a database lookup, for example).
Then in ViewPerson Activity, you can get the bundle of extra data, make sure it isn't null (in case if you sometimes don't pass data), then get the data.
Now if you need to share data between two Activities, you can also have a Global Singleton.
Then call it in any activity by:
Bryan DennyBryan Denny20.5k3030 gold badges9898 silver badges121121 bronze badges
Current responses are great but a more comprehensive answer is needed for beginners. There are 3 different ways to start a new activity in Android, and they all use the
Intent
class; Intent | Android Developers. - Using the
onClick
attribute of the Button. (Beginner) - Assigning an
OnClickListener()
via an anonymous class. (Intermediate) - Activity wide interface method using the
switch
statement. (Pro)
Here's the link to my example if you want to follow along: https://github.com/martinsing/ToNewActivityButtons
1. Using the onClick
attribute of the Button. (Beginner)
Buttons have an
onClick
attribute that is found within the .xml file: In Java class:
Advantage: Easy to make on the fly, modular, and can easily set multiple
onClick
s to the same intent. Disadvantage: Difficult readability when reviewing.
2. Assigning an OnClickListener()
via an anonymous class. (Intermediate)
This is when you set a separate
setOnClickListener()
to each button
and override each onClick()
with its own intent. In Java class:
Advantage: Easy to make on the fly.
Disadvantage: There will be a lot of anonymous classes which will make readability difficult when reviewing.
3. Activity wide interface method using the switch
statement. (Pro)
This is when you use a
switch
statement for your buttons within the onClick()
method to manage all the Activity's buttons. In Java class:
Advantage: Easy button management because all button intents are registered in a single
onClick()
methodFor the second part of the question, passing data, please see How do I pass data between Activities in Android application?
andreikashin46033 gold badges66 silver badges2121 bronze badges
Martin SingMartin Sing
When user clicks on the button, directly inside the XML like that:
Using the attribute
IntelliJ AmiyaIntelliJ Amiyaandroid:onClick
we declare the method name that has to be present on the parent activity. So I have to create this method inside our activity like that:55.9k1313 gold badges125125 silver badges143143 bronze badges
andy5,10022 gold badges1818 silver badges4444 bronze badges
vishal ranganivishal rangani
user1923551user1923551
Emmanuel,
I think the extra info should be put before starting the activity otherwise the data won't be available yet if you're accessing it in the onCreate method of NextActivity.
user1337489user1337489
From the sending Activity try the following code
From the receiving Activity try the following code:
Then just add the following code to the AndroidManifest.xml file
Alex IraborAlex Irabor20611 gold badge55 silver badges1414 bronze badges
MahtabMahtab
kleopatra45.5k1616 gold badges7676 silver badges165165 bronze badges
PrinkalPrinkal
joseph sarzjoseph sarz
The way to start new activities is to broadcast an intent, and there is a specific kind of intent that you can use to pass data from one activity to another. My recommendation is that you check out the Android developer docs related to intents; it's a wealth of info on the subject, and has examples too.
Brian DriscollBrian Driscoll16.7k22 gold badges3939 silver badges5656 bronze badges
Starting an activity from another activity is very common scenario among android applications.
To start an activity you need an Intent object.
To start an activity you need an Intent object.
How to create Intent Objects?
An intent object takes two parameter in its constructor
- Context
- Name of the activity to be started. (or full package name)
Example:
So for example,if you have two activities, say
HomeActivity
and DetailActivity
and you want to start DetailActivity
from HomeActivity
(HomeActivity-->DetailActivity).Here is the code snippet which shows how to start DetailActivity from
HomeActivity.
And you are done.
Coming back to button click part.
Rohit SinghRohit Singh4,14122 gold badges3333 silver badges3838 bronze badges
Start another activity from this activity and u can pass parameters via Bundle Object also.
Retrive data in another activity (YourActivity)
DroidNinjaDroidNinja
First Activity
Second Activity
Suggestion
Always put keys in constant file for more managed way.
KhemrajKhemraj20.4k1010 gold badges7474 silver badges102102 bronze badges
Carbonite one moment please in french. Implement the View.OnClickListener interface and override the onClick method.
user1918566user191856610611 gold badge22 silver badges1212 bronze badges
Although proper answers have been already provided but I am here for searching the answer in language Kotlin. This Question is not about language specific so I am adding the code to accomplish this task in Kotlin language.
Here is how you do this in Kotlin for andorid
Abdul RehmanAbdul Rehman
Jayesh PrajapatiJayesh Prajapati
When button is clicked:
To received the extra data from
Hasib AkterHasib AkterNextActivity.class
:4,78522 gold badges88 silver badges2828 bronze badges
Write the code in your first activity .
In secondActivity.class
Gyan Swaroop AwasthiGyan Swaroop Awasthi
Place button widget in xml like below
After that initialise and handle on click listener in Activity like below .
In Activity On Create method :
quant1,61211 gold badge1515 silver badges2727 bronze badges
prakash421prakash421
The Most simple way to open activity on button click is:
- Create two activities under the res folder, add a button to the first activity and give a name to
onclick
function. - There should be two java files for each activity.
- Below is the code:
MainActivity.java
SecondActivity.java
AndroidManifest.xml(Just add this block of code to the existing)
squaleLis2,96911 gold badge1515 silver badges2626 bronze badges
sn nsn n
protected by Community♦Nov 9 '13 at 11:54
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?