Learn Basic Concepts of Cursor and LayoutInflator in Android

Introduction

 
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.
  1. public ArrayList < HashMap < String, String >> fetchData()  
  2. {  
  3.  ArrayList < HashMap < String, String >> datalist = null;  
  4.  datalist = new ArrayList < HashMap < String, String >> ();  
  5.  SQLiteDatabase database = this.getReadableDatabase();  
  6.  String selectQuery = "SELECT * from Table_Account ";  
  7.  Cursor cursor = database.rawQuery(selectQuery, null);  
  8.  if (cursor.moveToFirst())   
  9.  {  
  10.   do   
  11.   {  
  12.    HashMap < String, String > map = new HashMap < String, String > ();  
  13.    map.put("amount", String.valueOf(cursor.getInt(0)));  
  14.    map.put("type", cursor.getString(1));  
  15.    map.put("date", cursor.getString(2));  
  16.    datalist.add(map);  
  17.   } while (cursor.moveToNext());  
  18.  }  
  19.  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. 
  1. public int totalBalance()  
  2. {  
  3.  int totalamount = 0;  
  4.  SQLiteDatabase database = this.getReadableDatabase();  
  5.  String selectQuery = "select amount from Table_Account";  
  6.  Cursor cursor = database.rawQuery(selectQuery, null);  
  7.  if (cursor.moveToFirst())   
  8.  {  
  9.   do   
  10.   {  
  11.    totalamount = totalamount + cursor.getInt(0);  
  12.   } while (cursor.moveToNext());  
  13.  }  
  14.  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:
  1. 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:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:id="@+id/relativeLayout"  
  5.     android:layout_height="match_parent"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     android:paddingBottom="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity">   
  11. </RelativeLayout> 
The following is the second XML file that will be ed to the layout with the help of the LayoutInflator class:
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:orientation="vertical"  
  4.               android:layout_width="match_parent"  
  5.               android:layout_height="match_parent">  
  6.    
  7.     <TextView android:layout_width="wrap_content"  
  8.             android:layout_height="wrap_content"  
  9.             android:text="LayoutInflator"  
  10.             android:textStyle="bold"  
  11.             android:textSize="50dp"  
  12.             android:layout_gravity="center_horizontal"  
  13.             />  
  14. </LinearLayout> 
MainActivity.java
  1. package com.layoutinflator;   
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.LayoutInflater;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.RelativeLayout;  
  9.    
  10. public class MainActivity extends Activity {  
  11.    
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.    
  17.         RelativeLayout relativelayout=(RelativeLayout)findViewById(R.id.relativeLayout);  
  18.    
  19.         LayoutInflater inflater=getLayoutInflater();  
  20.        View view=inflater.inflate(R.layout.second, relativelayout, false);  
  21.        relativelayout.addView(view);  
  22.     }  
  23.    
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.         // Inflate the menu; this adds items to the action bar if it is present.  
  27.         getMenuInflater().inflate(R.menu.main, menu);  
  28.         return true;  
  29.     }  
  30.    
  31. }


Similar Articles