ContactsContract data & ListView help


Last Updated:

  1. idiot2k1

    idiot2k1 Member This Topic's Starter

    Joined:
    Mar 30, 2010
    Messages:
    8
    Likes Received:
    0
    Hello Developers

    I am trying to create the functionality of displaying a contact list and allow the user to select one of their contacts. I am using some code from the ApiDemos sample program, where I can create a listview activity and display the contact name and addresses. I can do this just by passing the names, not the data itself. I also need to pass the phone number, but there is only two slots for data on the listview layout.

    My question, can anyone suggest a way to create a listview with contacts and attach the contact data to the view item?

    I've looked in the code of the ApiDemos and it looks like they use a Map collection and call back to the Map by the position of the clicked object, but how would I manipulate a contact's name, formatted address and phone number, and also how would I store that correctly in the Map?

    This is my code such far:

    Code (Text):
    1. [/FONT][/COLOR]
    2. [COLOR=#646464][FONT=Verdana]//Listactivity on create[/FONT][/COLOR]
    3. [COLOR=#646464][FONT=Verdana]@Override[/FONT][/COLOR]
    4. [B][COLOR=#7f0055][FONT=Verdana]protected[/FONT][/COLOR][/B][B][COLOR=#7f0055][FONT=Verdana]void[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana] onCreate(Bundle savedInstanceState) {[/FONT][/COLOR]
    5. [B][COLOR=#7f0055][FONT=Verdana]super[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana].onCreate(savedInstanceState);[/FONT][/COLOR]
    6. [COLOR=#3f7f5f][FONT=Verdana]// Get a cursor with all phones[/FONT][/COLOR]
    7. [COLOR=black][FONT=Verdana]Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]CONTENT_URI[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana],[/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]null[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana], [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]null[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana], [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]null[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana], [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]null[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana]);[/FONT][/COLOR]
    8. [COLOR=black][FONT=Verdana]startManagingCursor(c);[/FONT][/COLOR]
    9. [COLOR=#3f7f5f][FONT=Verdana]// Map Cursor columns to views defined in simple_list_item_2.[U]xml[/U][/FONT][/COLOR]
    10. [COLOR=black][FONT=Verdana]Log.[I]d[/I]([/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]TAG[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana], [/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Creating ListAdapter"[/FONT][/COLOR][COLOR=black][FONT=Verdana]);[/FONT][/COLOR]
    11. [COLOR=black][FONT=Verdana]ListAdapter adapter = [/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]new[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana] SimpleCursorAdapter([/FONT][/COLOR][B][COLOR=#7f0055][FONT=Verdana]this[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana],[/FONT][/COLOR]
    12. [COLOR=black][FONT=Verdana]android.R.layout.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]simple_list_item_2[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana], c, [/FONT][/COLOR]
    13. [B][COLOR=#7f0055][FONT=Verdana]new[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana] String[] { ContactsContract.CommonDataKinds.StructuredPostal.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]DISPLAY_NAME[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana], ContactsContract.CommonDataKinds.StructuredPostal.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]FORMATTED_ADDRESS[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana]}, [/FONT][/COLOR]
    14. [B][COLOR=#7f0055][FONT=Verdana]new[/FONT][/COLOR][/B][B][COLOR=#7f0055][FONT=Verdana]int[/FONT][/COLOR][/B][COLOR=black][FONT=Verdana][] { android.R.id.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]text1[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana],android.R.id.[/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]text2[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana]});[/FONT][/COLOR]
    15. [COLOR=black][FONT=Verdana]Log.[I]d[/I]([/FONT][/COLOR][I][COLOR=#0000c0][FONT=Verdana]TAG[/FONT][/COLOR][/I][COLOR=black][FONT=Verdana], [/FONT][/COLOR][COLOR=#2a00ff][FONT=Verdana]"Setting ListAdapter"[/FONT][/COLOR][COLOR=black][FONT=Verdana]+c);[/FONT][/COLOR]
    16. [COLOR=black][FONT=Verdana]setListAdapter(adapter);[/FONT][/COLOR]
    17. [COLOR=black][FONT=Verdana]}[/FONT][/COLOR]
    18. [COLOR=black][FONT=Verdana]


    I hope that makes sense.

    Thanks Much
    -Adam
     

    Advertisement

Share This Page

Loading...