How to make Android Database application (SQLite) Part 2

Assalam-u-Alaikum, Welcome again folks. Today in this post I will be continuing the previous tutorial on how to make your own android database application. Hope you understood the previous part. Now lets complete the tutorial!

In previous steps I have covered the basic functions of database that are used thoroughly in android sqlite database. From now on we will cover on how to insert into & view data from database.

Step 7(a)

Make new Class to view data from database. Same as Step 5 (present in part 1 of this tutorial).

Right click package -> New -> Java Class.

8 a- Making New Class for Getting Results

 

 

 

 

Step 7(b)

Name that class “GetResult”.

8 b- Naming Class for Getting Results

 

 

 

 

Step 8 (a)

Now make layout for Activity you just made “GetResult.java”. Expand “res” folder then “layout”.

res-> layout -> (Right Click layout -> Click New).

9 a - Making xml for Get Result

 

 

 

 

Step 8(b)

Name the layout file “get_result”. Remember the names of all layouts should be small caps.

9 b - Making xml for Get Result

 

 

 

 

 

Step 8(c)

After the layout is set & loaded, Drag & Drop “ListView” from elements present on Left hand side.
10

 

 

 

 

 

 

10 b

 

 

 

 

After Drag & Drop of ListView you will be seeing the structure as shown in above snapshot.

 

Step 8(d)

Now swap to Text from bottom left. & put the id of ListView as whatever you want to give I am giving its name as getResultList.

To give id in android write : android:id = “@+id/getResultList”

11 

 

 

 

 

Step 9 (a)

Now open GetResult.java file that is present inside the package folder.

1) Extend GetResult to Activity & import required packages by right clicking the GetResult extends Activity line.

2) Inside GetResult Class write “onCre” which then generates the dropdown, from options select onCreate(SavedInstanceState). This will generate the function in which you will be attaching & playing with your layout xml file.

12 a

 

 

 

 

12 b

 

 

 

 

Step 9 (b)

1) Now time to attach the xml(layout) file that you made as “get_result“. Write “setContentView(R.layout.get_result)”.

2) Make the variable of ListView  better to make it global. Link the ListView with the xml as

ListVariable = (ListView) findViewById(R.id.getResultList);

12 c

 

 

 

 

Step 9(c)

For Back button repeat the previous steps as you did for ListView.

For Showing data from database we will be getting help of data structure ArrayList. You can use other Data Structure technique such as List<String> etc. You can also use simple String but its much much better to use data structures.

For Java Data Structures download ebooks from:
Java Data Structures Ebooks

or check JavaTPoint site:
Java Data Structures – Java TPoint

1) ArrayList<String> data = new ArrayList<String>();// Make variable of ArrayList<String>

2) DBController db = new DBController(GetResult.this); // Make object of DBController Class

3) db.OpenDataBase();//Opens Database Connection

4) data = db.getData();// Gets data from database & store into variable named data

5) db.CloseDataBase(); // Closes Database Connection

ArrayAdapter:

The default way to show data into the ListView is through ArrayAdapter. Following code snippet is the default way to use this class.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data); // here data is the variable we are using from above.

DBResult.setAdapter(adapter); //DBResult is the variable name for ListView 

12 d

 

 

 

 

Step 10

Here comes the final step . Yet its final But most important step too.

1) Open manifest (mostly present above java folder).

2) Inside <application> tag link GetResult.java file so that your apps know the classes present inside it. To link the class follow the code below:

<activity

android:name =”.GetResult”

android:label = “Database Result”

/>

13

 

 

 

 

 

 

Outputs of the Program:

 

14 Output a

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Output(a)

14 Output b

 

 

 

 

 

Output(b)

 

14 Output cOutput(c)

 

 


<<
>>

Post Your Comment

[fbcomments]