Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional Android 2 Application Development
This is the forum to discuss the Wrox book Professional Android 2 Application Development, 2nd Edition by Reto Meier; ISBN: 978-0-470-56552-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Android 2 Application Development section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 26th, 2011, 12:34 PM
Registered User
 
Join Date: Jan 2011
Location: Santa Ana, CA
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Chapter 5 Contact Picker Example

Just thought I'd note a correction to some of the code in Ch. 5 for the ContactPicker example. I'm sure somebody here has already posted this but I was unable to find it.

In the onCreate function in the main ContactPicker Activity, the book has two erroneous lines.

Listed:
Code:
final Cursor c = managedQuery(data, null, null, null);
Correct:
Code:
final Cursor c = managedQuery(data, null, null, null, null); // Note the extra null
Listed:
Code:
String[] from = new String[] {People.NAME}; // Deprecated
Correct:
Code:
String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
This should remove the errors and allow you to proceed.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old January 26th, 2011, 01:17 PM
Registered User
 
Join Date: Jan 2011
Location: Santa Ana, CA
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Doh!

Nevermind, I think. I'm still having problems. Doh.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old August 7th, 2011, 09:15 AM
Nop Nop is offline
Registered User
 
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Upgrade to Android 2.3.3 ?

Nice info especially the replacement of the deprecated class.

But the code generate of Force Cloase on Android 2.3.3 : have the correction for the upgrade to android 2.3.3 ?

Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old November 22nd, 2011, 07:47 PM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I finally got it working by changing intent-filter to this:

<intent-filter >
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="vnd.android.cursor.dir/contact"/>
</intent-filter>

and when calling activity I set data like this:

intent.setData(Uri.parse("content://com.android.contacts/contacts"));

this way I am given options to choose my contact chooser or native contacts chooser.

not sure if this is entirely corect way, but at least it works
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #5 (permalink)  
Old November 29th, 2011, 05:23 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I'm just working through this as we speak so i'll verify if this works...cheers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #6 (permalink)  
Old November 30th, 2011, 05:42 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, i've managed to get it working despite my newb status. It's probably best if i just past the files in the following posts so that there is no confusion whatsoever...I spent a day doing background reading and crashed because i missed inserting + "/" + (see below).

P.S. I still need to reread about it all but at least it's now from the vantage point that the code actually works.

Cheers...
Code:
public class ContactPicker extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);
    
    Intent intent = getIntent();
    String dataPath = intent.getData().toString();
    
    final Uri data = Uri.parse(dataPath);
    final Cursor c = managedQuery(data, null, null, null, null);
        
    String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME};
    int[]  to = new int[] { R.id.itemTextView };
        
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                                                          R.layout.listitemlayout,
                                                          c,
                                                          from,
                                                          to);
    ListView lv = (ListView)findViewById(R.id.contactListView);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        // Move the cursor to the selected item
        c.moveToPosition(pos);
        // Extract the row id.
        int rowId = c.getInt(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
        // Construct the result URI.
        Uri outURI = Uri.parse(data.toString() + "/" + rowId);
        Intent outData = new Intent();
        outData.setData(outURI);
        setResult(Activity.RESULT_OK, outData);
        finish();
      }
    });
  }
}

public class ContentPickerTester extends Activity {

  public static final int PICK_CONTACT = 1;
	
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.contentpickertester);
	
    Button button = (Button)findViewById(R.id.pick_contact_button);
	      
    button.setOnClickListener(new OnClickListener() {
     public void onClick(View _view) {
        Intent intent = new Intent(Intent.ACTION_PICK,Uri.parse("content://com.android.contacts/contacts"));
        startActivityForResult(intent, PICK_CONTACT);  
      } 
    });
  }
  
  @Override 
  public void onActivityResult(int reqCode, int resCode, Intent data) {
    super.onActivityResult(reqCode, resCode, data);
        
    switch(reqCode) {
      case (PICK_CONTACT) : {
        if (resCode == Activity.RESULT_OK) {
          Uri contactData = data.getData();
          Cursor c = managedQuery(contactData, null, null, null, null);
          c.moveToFirst();
          String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
          TextView tv = (TextView)findViewById(R.id.selected_contact_textview);
          tv.setText(name);
        }
        break;
      }
    }
  }
}

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.paad.contactpicker"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".ContactPicker"
                  android:label="@string/app_name">
            <intent-filter>
              <action android:name="android.intent.action.PICK"></action>
              <category android:name="android.intent.category.DEFAULT"></category>
              <data android:mimeType="vnd.android.cursor.dir/contact"/>
            </intent-filter>
        </activity>
        <activity android:name=".ContentPickerTester" 
                android:label="Contact Picker Test">
          <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="7" />
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
</manifest>

Last edited by docuk : December 3rd, 2011 at 03:10 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9 - TRY IT OUT - Extending the Contact Form robbiehaas BOOK: Beginning ASP.NET 4 : in C# and VB 5 September 17th, 2010 07:44 AM
Ch. 5 - Contact Picker - Data Source not found eCubeH BOOK: Professional Android 2 Application Development 0 May 6th, 2010 02:53 AM
Contact form From Chapter 9 susu2009 BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 November 20th, 2009 07:49 AM
Chapter-5 on Intents:ContactPickerTester does not show the button "Pick a Contact" sunilm12 BOOK: Professional Android Application Development ISBN: 978-0-470-34471-2 3 April 15th, 2009 11:55 AM
Chapter 9 pps 314-316 Contact Form User Control Featheriver BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 1 February 2nd, 2009 02:19 AM



All times are GMT -4. The time now is 07:56 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
© 2011 John Wiley & Sons, Inc.