Android SimpleCursorAdapter example

In this Android tip, I am going to provide you an example on using SimpleCursorAdapter class that binds data to a ListView or GridView easily. The SimpleCursorAdapter is easy to map columns from a Cursor object with the fields defined in xml layout file. The xml layout file represents a single row design of the Listview or a single cell design of the GridView. The fields in the layout file must be TextViews or ImageViews.
When you instantiate a SimpleCursorAdapter, you need to pass to its constructor the current context, layout id, Cursor object, string array of columns of Cursor to use, integer array of fields of row or cell layout file to use, and a zero flag. Typically, at this time, the Cursor object is null. You will call the setAdapter() method of the ListView or GridView to bind data to that view. After you fetch data from a content provider, the Cursor is not null and you can update the Cursor of the adapter by using the changeCursor() method. In this example, the content provider is a table that stores thumbnails of images in the external storage of an Android device. The table can be obtained from this value: MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI. The query() method of the ContentResolver is used to fetch data from that table. It returns a Cursor object that contains the data to be bound to a GridView. The GridView widget is used to display the thumbnails in the two-dimensional grid.
To have a workable the example application on using SimpleCursorAdapter, you create a new Android project. The project name is AndExample. Then modify the activity_main.xml file to add a GridView as shown below.

<LinearLayout xmlns:android=""




Then create an xml layout file of the GridView. It contains an ImageView. The name of file is grid_layout.xml.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android=""
   android:orientation="horizontal" >


Here is the content of the MainActivity class that creates an instance of SimpleCursorAdapter, fetches data from the content provider, and displays the thumbnails in the GridView.

package com.example.andexample;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity{
// Columns to take data from the content provider
String[] projection={MediaStore.Images.Thumbnails._ID,MediaStore.Images.Thumbnails.DATA};
// Cursor column to use
String[] Cursor_Cols={MediaStore.Images.Thumbnails.DATA};
// Layout field to use
int[] Layout_Fiels={};
// Other variables
SimpleCursorAdapter adapter;
Uri url;
protected void onCreate(Bundle savedInstanceState) {

// Create an instance of SimpleCursorAdapter

adapter =new SimpleCursorAdapter(
        this,          // Current context
        R.layout.list_row, // Layout for a single row
        null,           // No Cursor yet
        Cursor_Cols,      // Cursor columns to use
        Layout_Fiels,     // Layout fields to use
        0             // No flags

// Make a reference to the Gridview
GridView gridView = (GridView) findViewById(;
// Bind data to the GridView

public void onStart(){
  // Get ContentResolver
  ContentResolver cr=getContentResolver();   
  // Get the Uri object pointing to the Thumbnails content provider on external storage
  Uri url=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
  // Read all rows from the table specified by the Uri object
  Cursor cursor=cr.query(url, projection, null, null, null);
  // Update cursor in the adapter



Posted by: Dara | post date: 10-25-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.