Hello,

I am learning about Content Providers and tried the following example from a
tutorial. This example performs a Google Maps search based on the home
address of a contact.

First, the various contacts are presented in a ListView, and then when the
user taps the entry of a particular contact, the application is supposed to
present the user with the location of the address on the map.

To access the latitude information, I used
android.provider.Contacts.ContactMethods.POSTAL_LOCATION_LATITUDE in a DB
query. However, Android is returning the email address of the contact
instead of the Home Address!

Can someone tell me, why this might be the case, and what I must change to
obtain the correct address?

Please see the code below (let me know if I should provide any more info):

--------------------

package com.msi.ibmtutorial;

import android.os.Bundle;
import android.app.ListActivity;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ListAdapter;
import android.net.Uri;
import android.content.Intent;
import android.widget.SimpleCursorAdapter;
import android.provider.BaseColumns;
import android.provider.Contacts;
import android.provider.Contacts.ContactMethods;
import android.provider.Contacts.People;

public class MobileServiceCallContacts extends ListActivity {

    final String tag = "MSCC";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //Cursor c = getContentResolver().query( People.CONTENT_URI,
        Cursor c = managedQuery( People.CONTENT_URI,
                                               null,
                                               null,
                                               null,
                                               null );
        startManagingCursor( c );

        Integer cols = new Integer( c.getColumnCount() );
        Log.i( "MSCC", "Number of cols is: " + cols.toString() );

        String[] people_name = new String[] { People.NAME };
        int[] text_id = new int[] { android.R.id.text1 };
        ListAdapter la = new SimpleCursorAdapter( this,

android.R.layout.simple_list_item_1,
                                                c,
                                                people_name,
                                                text_id );
        setListAdapter( la );

    }

    @Override
    protected void onListItemClick ( ListView lv, View v, int position, long
dbId ) {
        super.onListItemClick( lv, v, position, dbId );

        try {
            /* Obtain the details of this record by creating a Content
Provider.
             * The WHERE clause used in the call to managedQuery below is
for the Home address
             * and for the person record selected in the GUI.
             */
            //String[] cols = new String[] { "type", "name" };
            //String query_string = "type=1 and name=" + dbId;
            String cols[]= new String[] {
                            BaseColumns._ID,

Contacts.ContactMethods.POSTAL_LOCATION_LATITUDE,
                            People.NAME };
            String query_string = "type=" + dbId;
            Log.d( "MSCC", "query_string = " + query_string );

            Cursor c = managedQuery( ContactMethods.CONTENT_URI,
                                     cols,
                                     null, //query_string,
                                     null,
                                     null );
            startManagingCursor( c );

            if ( c.isBeforeFirst() ) {
                c.move( 1 );
                if ( !c.isFirst() )
                {
                    Throwable t = new Throwable();
                    showAlert( "MSCC", "No contacts methods available!", t
);
                    return;
                }
            }

            Integer nCols = new Integer( c.getColumnCount() );
            int i;
            for ( i=0; i<nCols; i++ ) {
                Log.d( "MSCC", "c.getString[" + i + "] = " + c.getString( i
) );
            }

            //String addr = c.getString( c.getColumnIndex( "aux_data" )
);
            String addr = c.getString( c.getColumnIndex(
                            ContactMethods.POSTAL_LOCATION_LATITUDE ) );
            Log.d( "MSCC", "addr = " + addr );


            addr = addr.replace( "\n", "" );
            addr = addr.replace( ",", "" );
            addr = addr.replace( " ", "+" );
            Uri uriContent1 = Uri.parse( "geo:0,0?q=" + addr );
            Intent geoIntent = new Intent( "android.intent.action.VIEW",
uriContent1 );
            startActivity( geoIntent );
        }
        catch ( Exception e ) {
            Log.i( tag, e.getMessage() );
        }
    }

    void showAlert( String tag, String msg, Throwable t ) {
        Log.e( tag, msg, t );
    }

}

--------------------

This is the output in LogCat:

08-24 18:46:25.554: DEBUG/MSCC(1160): c.getString[0] = 1
08-24 18:46:25.564: DEBUG/MSCC(1160): c.getString[1] = [email protected]
08-24 18:46:25.564: DEBUG/MSCC(1160): c.getString[2] = Aaa
08-24 18:46:25.574: DEBUG/MSCC(1160): addr = [email protected]

-----------------------

Thanks,
Pankaj Godbole,

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to