I'm relatively new to Android, and I'm having trouble figuring out how to read my contact's phone number. My code below reads the contact's name and places it into an address book (not in alphabetical order). What I have to do is create an address book with contacts and their phone number in the same line.
Any insight would be appreciated!
Code:
import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TurnToTech", "Project name - AddressBook");
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayAdapter<String> list = new ArrayAdapter<String>(this,
R.layout.activity_main)
while (cursor.moveToNext()) {
String name = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// I checked online and this is what is used to get the contact's number
// But this crashes my AndressBook app.
String phone = cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
list.add(name);
}
setListAdapter(list);
}
Any insight would be appreciated!