1

I have Contact app which displays all the names of the Contact, with an ArrayAdapter. This works fine but i want to to open the native contact app with the details of the contact whenever i click on a name.

Thank you in advance!

public class MainActivity extends ListActivity {


private CustomAdapter customAdapter;

List<String> contacts = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    ListView lv = (ListView) findViewById(android.R.id.list);
    customAdapter = new CustomAdapter(this);
    lv.setAdapter(customAdapter);

}

@Override
protected void onResume() {
    super.onResume();
    Log.d("TAG","onresume");
    loadApps();
}





public void loadApps(){

    //Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    Cursor cursor;
    ContentResolver contentResolver = getContentResolver();
    cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,null,
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " = 1",
            null,
            "UPPER(" + ContactsContract.Contacts.DISPLAY_NAME + ") ASC");





    customAdapter.clear();
    if (cursor.getCount() > 0) {
        cursor.moveToFirst();

        do {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            contacts.add(name);

        } while (cursor.moveToNext());
    }
    cursor.close();

    for (String names : contacts) {

        AppInfoItem appInfoItem = new AppInfoItem();

        appInfoItem.name = names;

        customAdapter.add(appInfoItem);

    }
}



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

    Intent intent = new Intent(Intent.ACTION_VIEW,
            Uri.parse("content://contacts/people/"));

    if (intent != null) {
        startActivity(intent);
    }

}
}

CustomArrayAdapter.class

public class CustomAdapter extends ArrayAdapter<String> {

LayoutInflater layoutInflater;


public CustomAdapter(Context context) {

    super(context, 0);

    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


}


@Override
public View getView(int position, View convertView, ViewGroup parent) {


    // Create cell and populate with data of the array
    if (convertView == null)
        convertView = layoutInflater.inflate(android.R.layout.simple_list_item_1, parent, false);

    AppInfoItem item = (AppInfoItem) getItem(position);
    //String name = names[position];

    TextView textView = (TextView) convertView.findViewById(android.R.id.text1);
    textView.setText(item.name);





    return convertView;
}


}

AppInfoItem.class

public class AppInfoItem {

String name;
String id;


}
  • Did you check this http://stackoverflow.com/questions/866769/how-to-call-android-contacts-list? – akhil_mittal May 12 '15 at 11:21
  • It seems like a duplicate question to me. – akhil_mittal May 12 '15 at 11:21
  • It's not a duplicate, the previous question wanted a way to pick a contact and return their name to the calling application. In this question the OP has already got a list of contacts and wants to call the built-in contacts app to display the full details of that contact. – Max Spencer May 12 '15 at 11:26

1 Answers1

0

I think you need to use an Intent with the ACTION_VIEW action and the intent data set to the contact's URI of the form

content://contacts/people/1

as shown in the first example here http://developer.android.com/reference/android/content/Intent.html#IntentStructure

Max Spencer
  • 1,701
  • 2
  • 12
  • 21
  • I have tried it your way, but the app crashes as soon as I click on a name, I guess the 1 stands for a specific ID or something,which I don't understand Here's the Code: Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/")); if (intent != null) { startActivity(intent); } – RatedNextGen May 12 '15 at 12:23
  • Yeah the 1 has to be replaced with the contact's ID. Can you update you question to include more of the code of your activity? Please try to leave out anything completely unrelated, but it would be useful to see where you're running that first portion of the code. – Max Spencer May 12 '15 at 12:28
  • How do I get the ID into the variable, do I have to run the cursor again in onListItemClicked or what's the best way? – RatedNextGen May 12 '15 at 12:37
  • you need `` permission to read contacts – EpicPandaForce May 12 '15 at 12:42
  • I'll be back in an hour and I'll take a look at your full code when I get back if there's no accepted answer by then. – Max Spencer May 12 '15 at 12:56