Cursor is a class that represents a two-dimensional table for the database. When you want to fetch data from the table we use a Cursor. So when you fetch data from a table using a select statement the database first creates a Cursor object. Now to retrieve data from cursor you need to traverse the data by calling moveToFirst() and moveToNext().
- moveToFirst() sets the cursor pointer to the first element of the table.
- moveToNext() sets the cursor pointer to the next element of the table.
You will get data by calling some methods provided by the cursor class.
To get String data you will call "getString(columnIndex)". columnIndex is an integer value that represents the beginning column to retrieve data from. For the first column, the columnIndex value is 0 and for the second column, it is 1 and so on.
To get integer data you will call "getInt(columnIndex)".
Example of a cursor that I have used in my application.
This method will fetch the data from the database. Here I have created a method "fetchData()" that returns the Arraylist that is of String type. To fetch data we will first create the object of SQLliteDatabase that calls the rawQuery() method. In the rawquery() method we will the select statement as a parameter. This method returns the cursor that we will store in a Cursor type variable.
- public ArrayList < HashMap < String, String >> fetchData()
- {
- ArrayList < HashMap < String, String >> datalist = null;
- datalist = new ArrayList < HashMap < String, String >> ();
- SQLiteDatabase database = this.getReadableDatabase();
- String selectQuery = "SELECT * from Table_Account ";
- Cursor cursor = database.rawQuery(selectQuery, null);
- if (cursor.moveToFirst())
- {
- do
- {
- HashMap < String, String > map = new HashMap < String, String > ();
- map.put("amount", String.valueOf(cursor.getInt(0)));
- map.put("type", cursor.getString(1));
- map.put("date", cursor.getString(2));
- datalist.add(map);
- } while (cursor.moveToNext());
- }
- return datalist;
- }
Now the cursor has all the data so to get the data we will traverse the cursor using the moveToFirst() method and moveToNext() method. Create a Hashmap to store values that will be returned by the cursor. The last do something to the arraylist because I need to show this on the ListView.
This method will add all the data
In this method I add all the column values to get the total amount. So I have created a variable of integer type and initialize it with zero. Now call getReadableDatabase(). After that to fetch column values use a select statement and this select statement to the rawQuery() method that returns the cursor. Traverse the cursor to get the data one by one and add it to the totalamount inside a do-while statement.
- public int totalBalance()
- {
- int totalamount = 0;
- SQLiteDatabase database = this.getReadableDatabase();
- String selectQuery = "select amount from Table_Account";
- Cursor cursor = database.rawQuery(selectQuery, null);
- if (cursor.moveToFirst())
- {
- do
- {
- totalamount = totalamount + cursor.getInt(0);
- } while (cursor.moveToNext());
- }
- return totalamount;
- }
Purpose of Cursor
A Cursor represents the result of a query and basically points to one row of the query result. This way Android can buffer the query results efficiently since it does not need to load all data into memory.
Layout Inflator
Layout Inflator is a class that takes an XML file as input and builds view objects from it. This class is never used directly; use getLayoutInflator() or getSystemServices() to retrieve a LayoutInflator object that is already attached with a current context. For example, to retrieve a layout inflator object:
- LayoutInflater inflater=getLayoutInflater();
Simply put, "inflate"
means to fill so "infalte()" is used to fill the layout with an XML file layout that you want to show. In the Inflate() method you will the XML as the parameter that you want to show.
Example of LayoutInflator
The following is the activity_main XML file that will contain your layout:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:id="@+id/relativeLayout"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- </RelativeLayout>
The following is the second XML file that will be ed to the layout with the help of the LayoutInflator class:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
-
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="LayoutInflator"
- android:textStyle="bold"
- android:textSize="50dp"
- android:layout_gravity="center_horizontal"
- />
- </LinearLayout>
MainActivity.java
- package com.layoutinflator;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.View;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- RelativeLayout relativelayout=(RelativeLayout)findViewById(R.id.relativeLayout);
-
- LayoutInflater inflater=getLayoutInflater();
- View view=inflater.inflate(R.layout.second, relativelayout, false);
- relativelayout.addView(view);
- }
-
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- }