I'm a complete noob in android app development, so I need some help from any expert out there. I found some code online to display the contact list when clicking on a EditText. I understand most of the code so far but the problem is when I select the contact, it just throws up a force close error.
What I would like it to do is, once clicking on a contact, the contact's name and number is added to the EditText with ";" as a separator.
Here's the code I'm working with:
package com.rmedia.android;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class ContactList extends Activity implements OnClickListener {
EditText ed;
int PICK_CONTACT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ed =(EditText)findViewById(R.id.editContact);
ed.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.editContact:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == PICK_CONTACT){
Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
cursor.moveToNext();
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast toast = Toast.makeText(this, "Contect LIST = "+name+" "+contactId, Toast.LENGTH_LONG);
toast.show();
}
}
}
Any help towards the right direction is greatly appreciated! Thanks!