Reputation: 454
I am trying to retrieve all contact numbers from my device. And I have written code segment for that. But I am facing an error in retrieving list of contact numbers. I am posting my code as well as error log here.
Thanks.
Code in onCreate of MainActivity
contactListText = (TextView) findViewById(R.id.ContactsListText);
ContactsProvider c = new ContactsProvider(this);
c.setContactsString();
String contactList = c.getContactString();
contactListText.setText(contactList);
ContactsProvider.java
public class ContactsProvider {
ArrayList<String> contactList;
Context context;
String name;
public ContactsProvider(Context context) {
this.context = context;
}
public ArrayList<String> getContactList() {
return contactList;
}
public void setContactsString() {
contactList = new ArrayList<String>();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = context.getContentResolver().query(uri, projection, null, null, null);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String number = people.getString(indexNumber);
contactList.add(number);
} while (people.moveToNext());
}
public String getContactString() {
if (contactList.size() > 0) {
StringBuilder contactString = new StringBuilder();
for (String n : contactList) {
if (n.length() >= 10 && !n.startsWith("0217")) {
n = n.replace(" ", "");
n = n.replace("+", "");
n = n.replace("-", "");
n = n.substring(n.length() - 10);
if (contactString.length() != 0) {
contactString.append(",");
}
contactString.append(n);
}
}
return contactString.toString();
} else {
return null;
}
}
}
LogCat
04-10 09:17:32.303: E/AndroidRuntime(1098): FATAL EXCEPTION: main
04-10 09:17:32.303: E/AndroidRuntime(1098): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.beproject.ourway/com.beproject.ourway.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.os.Handler.dispatchMessage(Handler.java:99)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.os.Looper.loop(Looper.java:137)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-10 09:17:32.303: E/AndroidRuntime(1098): at java.lang.reflect.Method.invokeNative(Native Method)
04-10 09:17:32.303: E/AndroidRuntime(1098): at java.lang.reflect.Method.invoke(Method.java:511)
04-10 09:17:32.303: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-10 09:17:32.303: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-10 09:17:32.303: E/AndroidRuntime(1098): at dalvik.system.NativeStart.main(Native Method)
04-10 09:17:32.303: E/AndroidRuntime(1098): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
04-10 09:17:32.303: E/AndroidRuntime(1098): at com.beproject.ourway.system.ContactsProvider.setContactsString(ContactsProvider.java:37)
04-10 09:17:32.303: E/AndroidRuntime(1098): at com.beproject.ourway.MainActivity.onCreate(MainActivity.java:25)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.Activity.performCreate(Activity.java:5104)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-10 09:17:32.303: E/AndroidRuntime(1098): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-10 09:17:32.303: E/AndroidRuntime(1098): ... 11 more
Upvotes: 0
Views: 189
Reputation: 14021
Codes here:
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
people.moveToFirst();
do {
String number = people.getString(indexNumber);
contactList.add(number);
} while (people.moveToNext());
are not right. Why did you call String number = people.getString(indexNumber);
? It's meaningless.
Try this:
while (people.moveToNext()){
String number = people.getString(people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactList.add(number);
}
Upvotes: 1
Reputation: 266
try {
Log.d(TAG, "Contacts: " + people.getCount());
people.moveToFirst();
while (!people.isAfterLast()) {
String number = people.getString(indexNumber);
people.add(number);
}
} catch (Exception ignored) {
}
Upvotes: 1