0

I am trying to populate a ListView (that is a fragment) into an activity that loads the Android phone contacts. And since I'm not familiar with the SQLite-style queries, I may have messed up the syntax because I got this error:

java.lang.RuntimeException: An error occurred while executing doInBackground()
Caused by: android.database.sqlite.SQLiteException: near "display_name": syntax error (code 1): , while compiling: SELECT _id, photo_thumb_uri, display_name, has_phone_number FROM view_contacts_restricted WHERE ((1)) AND ((photo_thumb_uri=?display_name=?has_phone_number=?)) ORDER BY display_nameASC

Two questions:

  1. Below is my fragment code and my logcat. Does anything look wrong? (What is causing the error?)

  2. I am not using a fragment xml layout (although I tried before, so commented-out code is still there), because I read that a CursorLoader creates its own layout. Is that a correct assumption?

ContactsFragment.java

package org.azurespot.practiceapp.addressbook;

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ContentUris;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.example.practiceapp.R;

/*
 * Partially from http://stackoverflow.com/questions/18199359/how-to-display-contacts-in-a-listview-in-android-for-android-api-11
 */

public class ContactsFragment extends ListFragment implements 
                                LoaderCallbacks<Cursor>{

    private CursorAdapter mAdapter;
    public ListView listView;
    public Cursor cursor;
    private android.content.Context context;
    public View view;
    public static Uri uri;

    public static final String[] FROM = { 
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
            ContactsContract.Contacts.HAS_PHONE_NUMBER };

    private static final int[] TO = { R.id.contact_thumbnail, 
        R.id.contact_name, R.id.contact_number };

    // columns requested from the database
    private static final String[] PROJECTION = {
            Contacts._ID,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
            ContactsContract.Contacts.HAS_PHONE_NUMBER,
    };

    // this goes in the CursorLoader parameter list, it filters
    // out only those contacts who have a phone number
    private static final String FILTER = 
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI + "=?" +
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + "=?" +
            ContactsContract.Contacts.HAS_PHONE_NUMBER + "=?";

    private static final String[] SELECTION_ARGS = {
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI,
            ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
            ContactsContract.Contacts.HAS_PHONE_NUMBER
    };


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

        // delete list if already there (old version)
        if (!(listView == null)){
            listView.setAdapter(null);
        }

        // create adapter once
        context = getActivity();
        int layout = android.R.layout.simple_list_item_1;
        Cursor c = null; // there is no cursor yet
        int flags = 0; // no auto-requery! Loader requeries.
        // put List in adapter
        mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);


    } // end onCreate


    // Empty public constructor, required by the system
    public ContactsFragment() {}

    // A UI Fragment must inflate its View (all fragments must override onCreateView)
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//            Bundle savedInstanceState) {
//        // Inflate the fragment layout
//        View view =  inflater.inflate(R.layout.fragment_list_view,
//            container, false);
//        listView = (ListView) view.findViewById(R.id.contact_list); 
//        
//        return view;
//    }

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

        // every time we start, use a list adapter
        setListAdapter(mAdapter);
//        // scroll faster
//        listView.setFastScrollEnabled(true);

        // and tell loader manager to start loading
        getLoaderManager().initLoader(0, null, this);

    }

    // a CursorLoader does a query in the background
    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // load from the "Contacts table"
        Uri contentUri = Contacts.CONTENT_URI;

        // no sub-selection, no sort order, simply every row
        // projection says we want just the _id and the name column
        return new CursorLoader(getActivity(),
                contentUri,
                PROJECTION,
                FILTER,
                SELECTION_ARGS,
                ContactsContract.Contacts.DISPLAY_NAME + "ASC");
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
         // Once cursor is loaded, give it to adapter
        mAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // Delete the reference to the existing Cursor,
        // so it can recycle it
        mAdapter.swapCursor(null);

    }
}

Logcat

12-26 20:45:16.398: I/PersonaManager(18479): getPersonaService() name persona_policy
12-26 20:45:16.478: D/skia(18479): GFXPNG PNG bitmap created width:48 height:48 bitmap id is 270 
12-26 20:45:16.478: E/MoreInfoHPW_ViewGroup(18479): Parent view is not a TextView
12-26 20:45:16.488: D/skia(18479): GFXPNG PNG bitmap created width:72 height:72 bitmap id is 271 
12-26 20:45:16.498: D/skia(18479): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 272 
12-26 20:45:16.508: D/skia(18479): GFXPNG PNG bitmap created width:144 height:144 bitmap id is 273 
12-26 20:45:16.558: I/Adreno-EGL(18479): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
12-26 20:45:16.558: I/Adreno-EGL(18479): OpenGL ES Shader Compiler Version: E031.24.00.08+13
12-26 20:45:16.558: I/Adreno-EGL(18479): Build Date: 03/20/14 Thu
12-26 20:45:16.558: I/Adreno-EGL(18479): Local Branch: 0320_AU200_patches
12-26 20:45:16.558: I/Adreno-EGL(18479): Remote Branch: 
12-26 20:45:16.558: I/Adreno-EGL(18479): Local Patches: 
12-26 20:45:16.558: I/Adreno-EGL(18479): Reconstruct Branch: 
12-26 20:45:16.638: D/OpenGLRenderer(18479): Enabling debug mode 0
12-26 20:45:17.678: I/PersonaManager(18479): getPersonaService() name persona_policy
12-26 20:45:17.688: E/MoreInfoHPW_ViewGroup(18479): Parent view is not a TextView
12-26 20:45:17.708: D/skia(18479): GFXPNG PNG bitmap created width:228 height:228 bitmap id is 274 
12-26 20:45:17.708: D/skia(18479): GFXPNG PNG bitmap created width:228 height:228 bitmap id is 275 
12-26 20:45:17.718: D/skia(18479): GFXPNG PNG bitmap created width:12 height:12 bitmap id is 276 
12-26 20:45:17.718: D/AbsListView(18479): Get MotionRecognitionManager
12-26 20:45:17.728: D/AbsListView(18479): onVisibilityChanged() is called, visibility : 8
12-26 20:45:17.728: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.728: D/AbsListView(18479): onVisibilityChanged() is called, visibility : 0
12-26 20:45:17.728: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.738: D/AbsListView(18479): onVisibilityChanged() is called, visibility : 4
12-26 20:45:17.738: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.738: D/AbsListView(18479): onVisibilityChanged() is called, visibility : 0
12-26 20:45:17.738: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.748: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.768: W/dalvikvm(18479): threadid=11: thread exiting with uncaught exception (group=0x41737da0)
12-26 20:45:17.768: E/AndroidRuntime(18479): FATAL EXCEPTION: AsyncTask #1
12-26 20:45:17.768: E/AndroidRuntime(18479): Process: com.example.practiceapp, PID: 18479
12-26 20:45:17.768: E/AndroidRuntime(18479): java.lang.RuntimeException: An error occured while executing doInBackground()
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.lang.Thread.run(Thread.java:841)
12-26 20:45:17.768: E/AndroidRuntime(18479): Caused by: android.database.sqlite.SQLiteException: near "display_name": syntax error (code 1): , while compiling: SELECT _id, photo_thumb_uri, display_name, has_phone_number FROM view_contacts_restricted WHERE ((1)) AND ((photo_thumb_uri=?display_name=?has_phone_number=?)) ORDER BY display_nameASC
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.ContentResolver.query(ContentResolver.java:464)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:65)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:43)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-26 20:45:17.768: E/AndroidRuntime(18479):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 20:45:17.768: E/AndroidRuntime(18479):    ... 3 more
12-26 20:45:17.778: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.818: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.858: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:17.868: D/AbsListView(18479): unregisterIRListener() is called 
12-26 20:45:18.398: D/AbsListView(18479): onDetachedFromWindow
12-26 20:45:18.398: D/AbsListView(18479): unregisterIRListener() is called 

Logcat after adding space to " ASC"

12-26 21:24:44.808: I/PersonaManager(25167): getPersonaService() name persona_policy
12-26 21:24:44.818: E/MoreInfoHPW_ViewGroup(25167): Parent view is not a TextView
12-26 21:24:44.848: D/skia(25167): GFXPNG PNG bitmap created width:228 height:228 bitmap id is 274 
12-26 21:24:44.848: D/skia(25167): GFXPNG PNG bitmap created width:228 height:228 bitmap id is 275 
12-26 21:24:44.858: D/skia(25167): GFXPNG PNG bitmap created width:12 height:12 bitmap id is 276 
12-26 21:24:44.858: D/AbsListView(25167): Get MotionRecognitionManager
12-26 21:24:44.868: D/AbsListView(25167): onVisibilityChanged() is called, visibility : 8
12-26 21:24:44.868: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.868: D/AbsListView(25167): onVisibilityChanged() is called, visibility : 0
12-26 21:24:44.868: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.868: D/AbsListView(25167): onVisibilityChanged() is called, visibility : 4
12-26 21:24:44.868: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.878: D/AbsListView(25167): onVisibilityChanged() is called, visibility : 0
12-26 21:24:44.878: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.878: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.898: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:44.908: W/dalvikvm(25167): threadid=11: thread exiting with uncaught exception (group=0x41737da0)
12-26 21:24:44.908: E/AndroidRuntime(25167): FATAL EXCEPTION: AsyncTask #1
12-26 21:24:44.908: E/AndroidRuntime(25167): Process: com.example.practiceapp, PID: 25167
12-26 21:24:44.908: E/AndroidRuntime(25167): java.lang.RuntimeException: An error occured while executing doInBackground()
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.lang.Thread.run(Thread.java:841)
12-26 21:24:44.908: E/AndroidRuntime(25167): Caused by: android.database.sqlite.SQLiteException: near "display_name": syntax error (code 1): , while compiling: SELECT _id, photo_thumb_uri, display_name, has_phone_number FROM view_contacts_restricted WHERE ((1)) AND ((photo_thumb_uri=?display_name=?has_phone_number=?)) ORDER BY display_name ASC
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:181)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:413)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.ContentResolver.query(ContentResolver.java:464)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:65)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.CursorLoader.loadInBackground(CursorLoader.java:43)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:312)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:69)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:57)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-26 21:24:44.908: E/AndroidRuntime(25167):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-26 21:24:44.908: E/AndroidRuntime(25167):    ... 3 more
12-26 21:24:44.918: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:45.008: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:45.008: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:45.148: D/AbsListView(25167): onDetachedFromWindow
12-26 21:24:45.158: D/AbsListView(25167): unregisterIRListener() is called 
12-26 21:24:46.978: I/Process(25167): Sending signal. PID: 25167 SIG: 9
Azurespot
  • 3,066
  • 3
  • 45
  • 73
  • issue seem to be this part of your query AND ((photo_thumb_uri=?display_name=?has_phone_number=?)) you need to separate it by OR/AND operator – Rajen Raiyarela Dec 27 '14 at 05:34
  • Okay, thanks Rajen. I just don't know what is wrong with them, those are my 3 constants in my query. I will look them over again. Do you think it is okay that I use them in `FROM`, `PROJECTION`, `FILTER`, `PROJECTION`, and `SELECTION_ARGS`? I was wondering if that too many repetitions. – Azurespot Dec 27 '14 at 05:36

1 Answers1

0

Space is required between ContactsContract.Contacts.DISPLAY_NAME and "ASC"

Change this

return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            FILTER,
            SELECTION_ARGS,
            ContactsContract.Contacts.DISPLAY_NAME + "ASC");

into

return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            FILTER,
            SELECTION_ARGS,
            ContactsContract.Contacts.DISPLAY_NAME + " ASC");
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59