Monday, December 3, 2012

Handle Item Click on Query Contacts database ListView

Up to last exercise of our Query Contacts database series, the contacts were displayed in a ListView with Display Name only.

It's modified in this exercise to handle item click for the ListView, such that we can retrieve more info of the clicked record. (Please notice that up to now, we haven't retrieve the details of the contacts such as phone number.)

Handle Item Click on Query Contacts database ListView


Modify the Java code of the main activity from last exercise, Query Contacts database, display in ListView. The layout have no change.
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.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.app.Activity;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

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,
    ContactsContract.Contacts.HAS_PHONE_NUMBER,
    ContactsContract.Contacts.LOOKUP_KEY};
  
  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);
        
        listContacts.setOnItemClickListener(myOnItemClickListener);
 }
 
 OnItemClickListener myOnItemClickListener
 = new OnItemClickListener(){

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   Cursor cursor = (Cursor)parent.getItemAtPosition(position);
   int item_ID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));
   String item_DisplayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
   int item_HasPhoneNumber = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
   
   String item_LookUp = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
   
   String item_PhoneNumber = "";
   if (item_HasPhoneNumber > 0){
    item_PhoneNumber = "Has phone number.";
    
   }else{
    item_PhoneNumber = "No number.";
   }
   
   String item = String.valueOf(item_ID) + ": " + item_DisplayName 
     + "\n" + item_PhoneNumber
     + "\nLOOKUP_KEY: " + item_LookUp;
   
   Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
   
  }
  
 };

}


download filesDownload the files.

Next:
- Access ContactsContract.CommonDataKinds.Phone via LOOKUP_KEY

Compare with: Query Contacts database using Loader, with search function.

1 comment:

  1. How can we get phone number on onclick event?

    ReplyDelete