Can someone help me figure out what is wrong with this code? I want to be able to add all the contacts associated with the request to an ArrayList. But using the add command makes the loop seem to override it and again makes size 1. But using the add method with index causes this error. 'IndexOutOfBoundsError. you are welcome. help me.
This is an error log.
08-14 21:36:29.893 20439-20439/? E/Zygote﹕ MountEmulatedStorage()
08-14 21:36:29.893 20439-20439/? E/Zygote﹕ v2
08-14 21:36:29.943 20439-20439/? E/SELinux﹕ [DEBUG] get_category: variable
seinfo: default sensitivity: NULL, cateogry: NULL
08-14 21:36:44.973 20439-20439/com.cmpe277.personalassistant E/MA﹕
search_intent_has_started
08-14 21:36:45.103 20439-20439/com.cmpe277.personalassistant E/DU﹕
startQuery has launched
08-14 21:36:45.103 20439-20439/com.cmpe277.personalassistant E/DU﹕ query =
chemist
08-14 21:36:45.383 20439-20439/com.cmpe277.personalassistant
E/CLoaderCallbacks﹕ Nothing is null?!
08-14 21:36:53.293 20439-20439/com.cmpe277.personalassistant E/MA﹕
search_intent_has_started
08-14 21:36:53.393 20439-20439/com.cmpe277.personalassistant E/DU﹕
startQuery has launched
08-14 21:36:53.393 20439-20439/com.cmpe277.personalassistant E/DU﹕ query =
leela ashok
08-14 21:36:53.533 20439-20439/com.cmpe277.personalassistant
E/CLoaderCallbacks﹕ Nothing is null?!
08-14 21:36:53.563 20439-20439/com.cmpe277.personalassistant
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cmpe277.personalassistant, PID: 20439
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at
java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.add(ArrayList.java:147)
at
com.cmpe277.personalassistant.ContactablesLoaderCallbacks.onLoadFinished(ContactablesLoaderCallbacks.java:117)
at com.cmpe277.personalassistant.ContactablesLoaderCallbacks.onLoadFinished(ContactablesLoaderCallbacks.java:22)
at android.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:483)
at android.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:451)
at android.content.Loader.deliverResult(Loader.java:144)
at android.content.CursorLoader.deliverResult(CursorLoader.java:109)
at android.content.CursorLoader.deliverResult(CursorLoader.java:42)
at android.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:265)
at android.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:92)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5832)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
This is my code:
package com.kishore_kumar.callbacks;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds;
import android.util.Log;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
public class ContactablesLoaderCallbacks implements
LoaderManager.LoaderCallbacks<Cursor> {
Context mContext;
int counter = 0;
public static final String QUERY_KEY = "query";
public static final String TAG = "CLoaderCallbacks";
public ContactablesLoaderCallbacks(Context context) {
mContext = context;
}
@Override
public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) {
query was extracted
String query = args.getString(QUERY_KEY);
Uri uri = Uri.withAppendedPath(
CommonDataKinds.Contactables.CONTENT_FILTER_URI, query);
String selection =
CommonDataKinds.Contactables.HAS_PHONE_NUMBER + " = " + 1;
String sortBy = CommonDataKinds.Contactables.LOOKUP_KEY;
return new CursorLoader(
mContext,
uri,
null,
means "all"
selection,
must match)
null,
subbed into selection.
sortBy);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
TextView tv = (TextView)
((Activity)mContext).findViewById(R.id.sample_output);
if(tv == null) {
Log.e(TAG, "TextView is null?!");
} else if (mContext == null) {
Log.e(TAG, "Context is null?");
} else {
Log.e(TAG, "Nothing is null?!");
}
tv.setText(mContext.getText(R.string.intro_message) + "\n\n");
if (cursor.getCount() == 0) {
return;
}
column index to pull
int phoneColumnIndex =
cursor.getColumnIndex(CommonDataKinds.Phone.NUMBER);
int emailColumnIndex =
cursor.getColumnIndex(CommonDataKinds.Email.ADDRESS);
int nameColumnIndex =
cursor.getColumnIndex(CommonDataKinds.Contactables.DISPLAY_NAME);
int lookupColumnIndex =
cursor.getColumnIndex(CommonDataKinds.Contactables.LOOKUP_KEY);
int typeColumnIndex =
cursor.getColumnIndex(CommonDataKinds.Contactables.MIMETYPE);
cursor.moveToFirst();
String lookupKey = "";
do {
String currentLookupKey = cursor.getString(lookupColumnIndex);
if (!lookupKey.equals(currentLookupKey)) {
String displayName = cursor.getString(nameColumnIndex);
tv.append(displayName + "\n");
lookupKey = currentLookupKey;
}
String mimeType = cursor.getString(typeColumnIndex);
if (mimeType.equals(CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
tv.append("\tPhone Number: " +
cursor.getString(phoneColumnIndex) + "\n");
ArrayList<String> mylist = new ArrayList<String>();
mylist.add(counter, cursor.getString(phoneColumnIndex));
adds an element to the list.
tv.append("size = "+mylist.size());
counter++;
} else if (mimeType.equals(CommonDataKinds.Email.CONTENT_ITEM_TYPE))
{
tv.append("");
}
Contactables.
for(String column : cursor.getColumnNames()) {
Log.d(TAG, column + column + ": " +
cursor.getString(cursor.getColumnIndex(column)) + "\n");
}
} while (cursor.moveToNext());
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
}
I'm a newbie. So please help me understand.
source
share