Thursday, November 29, 2012

Query Contacts database, display in ListView.

The exercise "Query Contacts database" display result in TextView. It's modified to display in ListView.

Query Contacts database, display in ListView.


Modify layout to have a ListView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ListView 
        android:id="@+id/conactlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


package com.example.androidquerycontacts;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
 
 ListView listContacts;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  listContacts = (ListView)findViewById(R.id.conactlist);
  
  Uri queryUri = ContactsContract.Contacts.CONTENT_URI;

  String[] projection = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME};
  
  String selection = ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL";
  
  CursorLoader cursorLoader = new CursorLoader(
            this, 
            queryUri, 
            projection, 
            selection, 
            null, 
            null);
  
  Cursor cursor = cursorLoader.loadInBackground();
  
  String[] from = {ContactsContract.Contacts.DISPLAY_NAME};
        int[] to = {android.R.id.text1};
        
        ListAdapter adapter = new SimpleCursorAdapter(
                this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                from, 
                to, 
                CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        listContacts.setAdapter(adapter);
 }

}


download filesDownload the files.

Compare with:
- List images in MediaStore.Images.Media

Next:
- Handle Item Click on Query Contacts database ListView

Wednesday, November 28, 2012

Query Contacts database, selection with AND to combine more cause.

The previous exercise demonstrate how to "Query Contacts database" for all contacts, "Search for contacts with phone number only" and "Search with selection of LIKE cause". To combine both cause to search for contacts has phone number with certain constraint in DISPLAY_NAME, combine both cause with "AND".

contacts has phone number with certain constraint in DISPLAY_NAME


Change:
String selection = ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL";

to:
String constraint = "Lau";
String selection =
ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1"
+ " AND "
+ ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + constraint + "%'";

Tuesday, November 27, 2012

Query Contacts database, search with selection of LIKE cause.

The previous exercise demonstrate how to "Query Contacts database" for all contacts. To search for contacts with certain constraint in DISPLAY_NAME, we can modify the selection using LIKE cause.

Change:
String selection = ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL";

to:
String constraint = "Lau";
String selection = ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%" + constraint + "%'";

Query Contacts database, search with selection of LIKE cause.


Android Design in Action: Responsive Design


The special episode focus on tablets and responsive design. Look at the following apps and discuss how they respond to differing device form factors:
-- Android Calendar 3:47
-- Pattrn 8:18
-- Pocket 11:33
-- TED 14:54
-- Google I/O 2012 18:28


Monday, November 26, 2012

Query Contacts database for contacts with phone number only

Last exercise "Query Contacts database" for all contacts. If you want to query contacts with phone number only, simple change the selection:

Change:
String selection = ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL";

to:
String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";

Query Contacts database for contacts with phone number only

Saturday, November 24, 2012

Query Contacts database

This exercise demonstrate how to query Contacts database directly, to retrieve _ID and DISPLAY_NAME.

Query Contacts database


package com.example.androidquerycontacts;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.support.v4.content.CursorLoader;
import android.app.Activity;
import android.database.Cursor;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 TextView textContacts;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  textContacts = (TextView)findViewById(R.id.contacts);
  
  Uri queryUri = ContactsContract.Contacts.CONTENT_URI;
  String[] projection = new String[] {
    ContactsContract.Contacts._ID,
    ContactsContract.Contacts.DISPLAY_NAME};
  String selection = ContactsContract.Contacts.DISPLAY_NAME + " IS NOT NULL";
  
  CursorLoader cursorLoader = new CursorLoader(
            this, 
            queryUri, 
            projection, 
            selection, 
            null, 
            null);
  
  Cursor cursor = cursorLoader.loadInBackground();
  
  int columnIndex_ID = cursor.getColumnIndex(Contacts._ID);
  int columnIndex_DISPLAYNAME = cursor.getColumnIndex(Contacts.DISPLAY_NAME);
  
  String myContacts = "Contacts:\n";
  
  while(cursor.moveToNext()){
   int id = cursor.getInt(columnIndex_ID);
   String displayName = cursor.getString(columnIndex_DISPLAYNAME);
   myContacts += String.valueOf(id) + " : " + displayName +"\n";
  }
  
  textContacts.setText(myContacts);
 }


}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
        android:id="@+id/contacts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </ScrollView>
    

</LinearLayout>


Permission of "android.permission.READ_CONTACTS" is needed in AndroidManifest.xml.

download filesDownload the files.


Next:
- Query Contacts database for contacts with phone number only
- Query Contacts database, search with selection of LIKE cause
- Query Contacts database, selection with AND to combine more cause
- Query Contacts database, display in ListView
- Handle Item Click on Query Contacts database ListView
- Access ContactsContract.CommonDataKinds.Phone via LOOKUP_KEY
- Get Email from Contacts database

Friday, November 23, 2012

Get phone number from Contacts database

In the exercise, start a activity with intent of Intent.ACTION_PICK. Retrieve the phone number from the selected contact using SQLite query.

Get phone number from Contacts database


package com.example.androidreadcontact;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

public class MainActivity extends Activity {
 
 Button buttonReadContact;
 TextView textPhone;
 
 final int RQS_PICKCONTACT = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  buttonReadContact = (Button)findViewById(R.id.readcontact);
  textPhone = (TextView)findViewById(R.id.phone);
  
  buttonReadContact.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View arg0) {
    //Start activity to get contact
    final Uri uriContact = ContactsContract.Contacts.CONTENT_URI;
    Intent intentPickContact = new Intent(Intent.ACTION_PICK, uriContact);
    startActivityForResult(intentPickContact, RQS_PICKCONTACT);
   }});

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  if(resultCode == RESULT_OK){
   if(requestCode == RQS_PICKCONTACT){
    Uri returnUri = data.getData();
    Cursor cursor = getContentResolver().query(returnUri, null, null, null, null);
    
    if(cursor.moveToNext()){
     int columnIndex_ID = cursor.getColumnIndex(ContactsContract.Contacts._ID);
     String contactID = cursor.getString(columnIndex_ID);
     
     int columnIndex_HASPHONENUMBER = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
     String stringHasPhoneNumber = cursor.getString(columnIndex_HASPHONENUMBER);
     
     if(stringHasPhoneNumber.equalsIgnoreCase("1")){
      Cursor cursorNum = getContentResolver().query(
        ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
        null, 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactID, 
        null, 
        null);
      
      //Get the first phone number
      if(cursorNum.moveToNext()){
       int columnIndex_number = cursorNum.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
       String stringNumber = cursorNum.getString(columnIndex_number);
       textPhone.setText(stringNumber);
      }
      
     }else{
      textPhone.setText("NO Phone Number");
     }
     
     
    }else{
     Toast.makeText(getApplicationContext(), "NO data!", Toast.LENGTH_LONG).show();
    }
   }
  }
 }
 
 

}


Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/readcontact"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="Read Contact"/>
    <TextView
        android:id="@+id/phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Permission of "android.permission.READ_CONTACTS" is needed in AndroidManifest.xml.

download filesDownload the files.

Wednesday, November 14, 2012

Free ebook: Introducing Windows 8: An Overview for IT Professionals (Final Edition) - from Microsoft



Microsoft Press have released the final version of the ebook Introducing Windows 8: An Overview for IT Professionals, for FREE!

Link: Download PDF

Get a headstart evaluating Window 8—guided by a Windows expert who’s worked extensively with the software since the preview releases. Based on final, release-to-manufacturing (RTM) software, this book introduces new features and capabilities, with scenario-based insights demonstrating how to plan for, implement, and maintain Windows 8 in an enterprise environment. Get the high-level information you need to begin preparing your deployment now.

Topics include:

• Performance, reliability, and security features
• Deployment options
• Windows Assessment and Deployment Kit
• Windows PowerShell™ 3.0 and Group Policy
• Managing and sideloading apps
• Internet Explorer® 10
• Virtualization, Client Hyper-V, and Microsoft Desktop Optimization Pack
• Recovery features


Source: http://blogs.msdn.com/b/microsoft_press/archive/2012/11/13/free-ebook-introducing-windows-8-an-overview-for-it-professionals-final-edition.aspx

Tuesday, November 13, 2012

Android 4.2 SDK is available



Android 4.2 (Jelly Bean) SDK platform with API level 17 is available now. get started developing and testing, download the Android 4.2 Platform from the Android SDK Manager.

For a complete overview of what's new, take a look at the Android 4.2 platform highlights or read more of the details in the API overview.

Source: Android Developers Blog - Introducing Android 4.2, A New and Improved Jelly Bean.

Monday, November 12, 2012

Implement custom multi select ListView with custom ArrayAdapter

The post "Multi Choice ListView" demonstrate how to implement multi select ListView with build-in layout with simple TextView. This exercise demonstrate how to implement multi select ListView with custom layout, using custom ArrayAdapter.

custom multi select ListView


Create /res/layout/row.xml to define the layout of rows in our custom ListView.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <ImageView 
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher"/>
    <CheckedTextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeightSmall"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"/>
</LinearLayout>


Layout of our example.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/getresult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Get Result"/>
    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Main Java code.
package com.example.androidmultichoicelist;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 ListView myListView;
 Button getResult;
 
 private ArrayList<String> dayOfWeekList = new ArrayList<String>();

 private void initDayOfWeekList(){
  dayOfWeekList.add("Sunday");
  dayOfWeekList.add("Monday");
  dayOfWeekList.add("Tuesday");
  dayOfWeekList.add("Wednesday");
  dayOfWeekList.add("Thursday");
  dayOfWeekList.add("Friday");
  dayOfWeekList.add("Saturday");
  
 }
 
 MyArrayAdapter myArrayAdapter;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initDayOfWeekList();
        setContentView(R.layout.activity_main);
        
        myListView = (ListView)findViewById(R.id.list);

        myArrayAdapter = new MyArrayAdapter(
          this,
          R.layout.row,
          android.R.id.text1,
          dayOfWeekList
          );
        
        myListView.setAdapter(myArrayAdapter);
        myListView.setOnItemClickListener(myOnItemClickListener);
        
        getResult = (Button)findViewById(R.id.getresult);
        getResult.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    String result = "";
    
    /*
    //getCheckedItemPositions
    List<Integer> resultList = myArrayAdapter.getCheckedItemPositions();
    for(int i = 0; i < resultList.size(); i++){
     result += String.valueOf(resultList.get(i)) + " ";
    }
    */
    
    //getCheckedItems
    List<String> resultList = myArrayAdapter.getCheckedItems();
    for(int i = 0; i < resultList.size(); i++){
     result += String.valueOf(resultList.get(i)) + "\n";
    }
    
    myArrayAdapter.getCheckedItemPositions().toString();
    Toast.makeText(
      getApplicationContext(), 
      result, 
      Toast.LENGTH_LONG).show();
   }});
        
    }
    
    OnItemClickListener myOnItemClickListener
    = new OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   myArrayAdapter.toggleChecked(position);
   
  }};
    
    private class MyArrayAdapter extends ArrayAdapter<String>{
     
     private HashMap<Integer, Boolean> myChecked = new HashMap<Integer, Boolean>();

  public MyArrayAdapter(Context context, int resource,
    int textViewResourceId, List<String> objects) {
   super(context, resource, textViewResourceId, objects);
   
   for(int i = 0; i < objects.size(); i++){
    myChecked.put(i, false);
   }
  }
     
  public void toggleChecked(int position){
   if(myChecked.get(position)){
    myChecked.put(position, false);
   }else{
    myChecked.put(position, true);
   }
   
   notifyDataSetChanged();
  }
  
  public List<Integer> getCheckedItemPositions(){
   List<Integer> checkedItemPositions = new ArrayList<Integer>();
   
   for(int i = 0; i < myChecked.size(); i++){
    if (myChecked.get(i)){
     (checkedItemPositions).add(i);
    }
   }
   
   return checkedItemPositions;
  }
  
  public List<String> getCheckedItems(){
   List<String> checkedItems = new ArrayList<String>();
   
   for(int i = 0; i < myChecked.size(); i++){
    if (myChecked.get(i)){
     (checkedItems).add(dayOfWeekList.get(i));
    }
   }
   
   return checkedItems;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   
   if(row==null){
    LayoutInflater inflater=getLayoutInflater();
    row=inflater.inflate(R.layout.row, parent, false);  
   }
   
   CheckedTextView checkedTextView = (CheckedTextView)row.findViewById(R.id.text1);
   checkedTextView.setText(dayOfWeekList.get(position));
   
   Boolean checked = myChecked.get(position);
   if (checked != null) {
    checkedTextView.setChecked(checked);
            }
   
   return row;
  }
  
    }

}


download filesDownload the files.

Thursday, November 1, 2012

List MediaStore.Images.Thumbnails in GridView, with custom SimpleCursorAdapter.

Last exercise list MediaStore.Images.Thumbnails in ListView, with custom SimpleCursorAdapter. It's easy to be modified to display in GridView.

List MediaStore.Images.Thumbnails in GridView, with custom SimpleCursorAdapter.


Modify /res/layout/row.xml to have a ImageView only in each grid.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    
    <ImageView
        android:id="@+id/thumb"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

</LinearLayout>


Modify layout to have a GridView.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="100dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"/>

</LinearLayout>


Modify the code extends Activity with GridView, instead of ListActivity.
package com.example.androidlistimages;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {
 
 //define source of MediaStore.Images.Media, internal or external storage
 final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
 final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
 final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;

 //SimpleCursorAdapter mySimpleCursorAdapter;
 MyAdapter mySimpleCursorAdapter;
 
 GridView myGridView;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        myGridView = (GridView)findViewById(R.id.gridview);
        
        String[] from = {MediaStore.MediaColumns.TITLE};
        int[] to = {android.R.id.text1};

        CursorLoader cursorLoader = new CursorLoader(
          this, 
          sourceUri, 
          null, 
          null, 
          null, 
          MediaStore.Audio.Media.TITLE);
        
        Cursor cursor = cursorLoader.loadInBackground();
        
        mySimpleCursorAdapter = new MyAdapter(
          this, 
          android.R.layout.simple_list_item_1, 
          cursor, 
          from, 
          to, 
          CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        
        myGridView.setAdapter(mySimpleCursorAdapter);
        myGridView.setOnItemClickListener(myOnItemClickListener);
    }
    
    OnItemClickListener myOnItemClickListener
    = new OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   Cursor cursor = mySimpleCursorAdapter.getCursor();
   cursor.moveToPosition(position);

   int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
   getThumbnail(int_ID);
  }};
 
 private Bitmap getThumbnail(int id){

  String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};

  CursorLoader thumbCursorLoader = new CursorLoader(
          this, 
          thumbUri, 
    thumbColumns, 
    thumb_IMAGE_ID + "=" + id, 
    null, 
    null);
        
        Cursor thumbCursor = thumbCursorLoader.loadInBackground();
        
        Bitmap thumbBitmap = null;
        if(thumbCursor.moveToFirst()){
   int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
   
   String thumbPath = thumbCursor.getString(thCulumnIndex);
   
   Toast.makeText(getApplicationContext(), 
     thumbPath, 
     Toast.LENGTH_LONG).show();
   
   thumbBitmap = BitmapFactory.decodeFile(thumbPath);
   
   //Create a Dialog to display the thumbnail
   AlertDialog.Builder thumbDialog = new AlertDialog.Builder(MainActivity.this);
   ImageView thumbView = new ImageView(MainActivity.this);
   thumbView.setImageBitmap(thumbBitmap);
   LinearLayout layout = new LinearLayout(MainActivity.this);
         layout.setOrientation(LinearLayout.VERTICAL);
         layout.addView(thumbView);
         thumbDialog.setView(layout);
         thumbDialog.show();
   
  }else{
   Toast.makeText(getApplicationContext(), 
     "NO Thumbnail!", 
     Toast.LENGTH_LONG).show();
  }
        
        return thumbBitmap;
 }
 
 public class MyAdapter extends SimpleCursorAdapter{
  
  Cursor myCursor;
     Context myContext;

  public MyAdapter(Context context, int layout, Cursor c, String[] from,
    int[] to, int flags) {
   super(context, layout, c, from, to, flags);
   
   myCursor = c;
      myContext = context;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   View row = convertView;
   if(row==null){
    LayoutInflater inflater=getLayoutInflater();
    row=inflater.inflate(R.layout.row, parent, false); 
   }
   
   ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
   
   myCursor.moveToPosition(position);
   
   int myID = myCursor.getInt(myCursor.getColumnIndex(MediaStore.Images.Media._ID));
   
   String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};
   CursorLoader thumbCursorLoader = new CursorLoader(
     myContext, 
           thumbUri, 
     thumbColumns, 
     thumb_IMAGE_ID + "=" + myID, 
     null, 
     null);
   Cursor thumbCursor = thumbCursorLoader.loadInBackground();
   
   Bitmap myBitmap = null;
         if(thumbCursor.moveToFirst()){
    int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
    String thumbPath = thumbCursor.getString(thCulumnIndex);
    myBitmap = BitmapFactory.decodeFile(thumbPath);
    thumbV.setImageBitmap(myBitmap);
   }
   
   return row;
  }
  
 }
}


download filesDownload the files.