Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8784209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:01:43+00:00 2026-06-13T21:01:43+00:00

Can anyone point me to a simple example of using a CursorLoader to query

  • 0

Can anyone point me to a simple example of using a CursorLoader to query a SQLite database and populate a ListFragment? The code below will compile, but when I run it, LogCat tells me that “ListFrag” cannot be cast LoaderManager.LoaderCallbacks. If I change ListFrag so that it’s not ListFrag, just ListFrag, I’m told that “ListFrag cannot be cast to android.v4.support.Fragment”. Note that my activity extends FragmentActivity and my Fragment extends ListFragment because of the information in this forum post. I’ve been struggling with getting this to work for a while now and I just don’t get it. Here’s my code for the activity which contains the fragment and thank you:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity

public class MyList extends FragmentActivity {
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myfragment);
}
}

And here’s my code for the fragment:

import android.app.ListFragment;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.app.LoaderManager;
import android.view.View;
import android.widget.ListView;

@SuppressWarnings("hiding")
public class ListFrag<Cursor> extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {   
    private static final String TABLE_BASEPATH = "MyTable_tbl";
    private static final String AUTHORITY = "SQLData";
    public static final Uri MY_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_BASEPATH);
    private static final String[] PROJECTION = new String[] { "_id", "fieldname" };
    private SimpleCursorAdapter mAdapter;
    private static final int LOADER_ID = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        Intent myData = getActivity().getIntent();
        Bundle info = myData.getExtras();


            SimpleCursorAdapter adapter;
            String[] dataColumns = { "fieldname" };
            int[] viewIDs = { R.id.mydetails };
            adapter = new SimpleCursorAdapter(getActivity(), R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);
            getLoaderManager().initLoader(0, info, (LoaderCallbacks<Cursor>) this); 

    }

          @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String item = (String) getListAdapter().getItem(position);
        DetailFrag frag = (DetailFrag) getFragmentManager().findFragmentById(R.id.frag_detail);
        if (frag != null && frag.isInLayout()) {
            frag.setText(item);
        }
    }

    @SuppressWarnings("unchecked")
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String selection = "level='" + args.getString("Level") + "'";
        return (Loader<Cursor>) new CursorLoader(getActivity(), MY_URI,
                PROJECTION, selection, null, null); 
    }
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        switch (loader.getId()) {
          case LOADER_ID:
            mAdapter.swapCursor((android.database.Cursor) cursor);
            break;
        }


    }
    public void onLoaderReset(Loader<Cursor> loader) {
        mAdapter.swapCursor(null);

    }

}

Just saw your comment about LogCat. Oops. Here’s my entire LogCat:

11-05 15:47:27.953: D/dalvikvm(553): Not late-enabling CheckJNI (already on)
11-05 15:47:28.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:28.683: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:29.143: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:29.153: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:29.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:29.653: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:30.143: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:30.153: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:30.643: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:30.693: D/gralloc_goldfish(553): Emulator without GPU emulation detected.
11-05 15:47:30.693: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:32.843: E/ActivityThread(553): Failed to find provider info for SQLData
11-05 15:47:32.883: D/AndroidRuntime(553): Shutting down VM
11-05 15:47:32.883: W/dalvikvm(553): threadid=1: thread exiting with uncaught exception    (group=0x409c01f8)
11-05 15:47:32.903: E/AndroidRuntime(553): FATAL EXCEPTION: main
11-05 15:47:32.903: E/AndroidRuntime(553): java.lang.NullPointerException
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.MyKnitCards.project.ListFrag.onLoadFinished(ListFrag.java:71)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:425)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:393)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.Loader.deliverResult(Loader.java:103)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:81)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:221)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.os.Looper.loop(Looper.java:137)
11-05 15:47:32.903: E/AndroidRuntime(553):  at android.app.ActivityThread.main(ActivityThread.java:4424)
11-05 15:47:32.903: E/AndroidRuntime(553):  at java.lang.reflect.Method.invokeNative(Native Method)
11-05 15:47:32.903: E/AndroidRuntime(553):  at java.lang.reflect.Method.invoke(Method.java:511)
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-05 15:47:32.903: E/AndroidRuntime(553):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-05 15:47:32.903: E/AndroidRuntime(553):  at dalvik.system.NativeStart.main(Native Method)
11-05 15:47:33.213: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:33.233: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
11-05 15:47:33.513: I/dalvikvm(553): threadid=3: reacting to signal 3
11-05 15:47:33.533: I/dalvikvm(553): Wrote stack traces to '/data/anr/traces.txt'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:01:45+00:00Added an answer on June 13, 2026 at 9:01 pm

    First try switching these imports to the support library versions. Change:

    import android.app.ListFragment;
    import android.app.LoaderManager.LoaderCallbacks;
    

    to:

    import android.support.v4.app.ListFragment;
    import android.support.v4.app.LoaderManager.LoaderCallbacks;
    

    Next make sure you are asking for the support fragment manager:

    DetailFrag frag = (DetailFrag) getActivity().getSupportFragmentManager().findFragmentById(R.id.frag_detail);
    //                                              ^^^^^^^
    

    (This might not solve everything, if you still need help post the LogCat errors from these changes so we can see exactly what is happening.)


    Addition
    mAdapter is null, change adapter to mAdapter here:

    mAdapter = new SimpleCursorAdapter(getActivity(), R.layout.mylist, null, dataColumns, viewIDs, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can Anyone point Me in the direction of how to tell when, for example,
Can anyone point me at an example of a customized Google Chrome Frame install
Can anyone point me toward a Go library that will give me the ability
Can anyone point me to an example on how to use CURRENT_DATE in a
Can anyone point me to a WCF Self-Hosted NetTCP example that works on Monotouch?
Can anyone point me to an example of or briefly describe how one would
Can anyone help me with sample code for a compass needle that points in
Can anyone point me to some documentation about how I can use an Iso
can anyone point to a site or describe how I can go about designing
Can anyone point me at a decent repository or dependency injection library for any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.