 |
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 26th, 2011, 01:34 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

January 26th, 2011, 02:17 PM
|
|
Registered User
|
|
Join Date: Jan 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Doh!
Nevermind, I think. I'm still having problems. Doh.
|
|

August 7th, 2011, 09:15 AM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 22nd, 2011, 08:47 PM
|
|
Registered User
|
|
Join Date: Nov 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

November 29th, 2011, 06:23 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I'm just working through this as we speak so i'll verify if this works...cheers
|
|

November 30th, 2011, 06:42 PM
|
|
Registered User
|
|
Join Date: Aug 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 04:10 PM..
|
|
 |