1. Download our Official Android App: Forums for Android!

Apps contact list to listview

Discussion in 'Android Development' started by thejeraldo, Sep 21, 2011.

  1. thejeraldo

    thejeraldo Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    5
    Joined:
    Sep 21, 2011

    Sep 21, 2011
    5
    0
    5
    hi, im trying to do a contact list using by using a list view.
    im able to use the contactcontracts provider and was able to fetch all the contacts from my phone but only through logcat. how do i do this/pass the values i fetch to an array or a listview? thanks. heres my code.

    package com.olecontacts.sirje;

    import android.app.Activity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.provider.ContactsContract.PhoneLookup;
    import android.util.Log;

    public class OleContactsActivity extends Activity{
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
    Cursor people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
    //String[] fields = new String[] {ContactsContract.Data.DISPLAY_NAME};
    while(people.moveToNext()){
    int nameIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
    String name = people.getString(nameIndex);
    Log.d("CONTACTS", name);
    }
    }
    }
     

    Advertisement

  2. miXer

    miXer Android Enthusiast
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    To do it in a listview you must:
    1. Edit your main.xml file to insert a ListView
    2. Create a new xml file whitch will be the list item
    3. Edit you OleContactsActivity.java file and instead of extending Activity, it must extend ListActivity.
    4. Create a SimpleCursorAdapter and assign it to the listview.

    Search on google for tutorials on ListViews.
     

Share This Page

Loading...