Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

android - How to show phone contacts in a ListView

Following is my code, actually on screen it's not showing me any contact. In emulator I have 5 contacts added. Please tell me what to do.

{
    //some code
    Cursor cur = getContacts();
    String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
    SimpleCursorAdapter adapter = 
        new SimpleCursorAdapter(this, 
                                R.layout.list_view_item_new,
                                cur,
                                fields,
                                new int[] {R.id.contactEntryText});
    lv.setAdapter(adapter);
}

private Cursor getContacts() {  
    // Run query     
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = 
        new String[]{ ContactsContract.Contacts._ID,
                      ContactsContract.Contacts.DISPLAY_NAME }; 
    String selection = null;
    String[] selectionArgs = null;  
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
        " COLLATE LOCALIZED ASC";  
    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);  
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I have copied and executed almost the same code and it works:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Cursor cur = getContacts();

        ListView lv = getListView();

       String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};

       SimpleCursorAdapter adapter = 
                new SimpleCursorAdapter(this, 
                                        R.layout.main,
                                        cur,
                                        fields,
                                        new int[] {R.id.txtbox});
          lv.setAdapter(adapter);         
    }    

    private Cursor getContacts() {  
        // Run query     
        Uri uri = ContactsContract.Contacts.CONTENT_URI;

        String[] projection = 
                new String[]{ ContactsContract.Contacts._ID,
                              ContactsContract.Contacts.DISPLAY_NAME }; 
            String selection = null;
            String[] selectionArgs = null;  
            String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + 
                " COLLATE LOCALIZED ASC";  
            return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

Please check if you have done anything wrong in the textview implementation?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...