• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps A problem with managedQuery

Letfly

Lurker
Hi!
Such code

package com.app.whatisylife;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Contacts.People;
@SuppressWarnings("deprecation")
public class ContactClass
{
String[] projection = new String[] {
People._ID,
People._COUNT,
People.NAME,
People.NUMBER
};
Cursor contactCursor;
Uri contacts;
void getContactData()
{
contactCursor = managedQuery(contacts,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
}
}
string "contactCursor = managedQuery" causes error "the metod managedQuery is undefined for the type ContactClass. What should I import in file? Or may be its a problem of class description? Help please!
 
Want to specify. I need an "independent" class that doesn't extends Activity and uses managedQuery or CursorLoader. I want to create object of this class in onCreate and work with it. Is it possible approximately in such way?
package com.app.whatisylife;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

public class ContactClass
{
String[] projection = new String[]
{
"_ID",
"_COUNT",
"NAME",
"NUMBER"
};
private Context appContext;
private Cursor contactCursor;
private Uri contacts;

ContactClass()
{
appContext=getApplicationContext();
}

void getContactData()
{

Cursor cursor = CursorLoader()
}

}
Here is a similar error "CursorLoader is undefined.
 
My bad. CursorLoader is only available from API level 11(Honeycomb(3.0)), so unless this package will be used on such a device, you must use managedQuery or query methods.

- To use managedQuery you must extend Activity because by calling managedQuery, Android takes care of handling the life cycle of the Cursor returned.

- Another method you can use is query from Context. With this you don't have to extend anything, just pass a Context in to the method. This method will not handle the life cycle for you, and it can be used like this:

Code:
void getContactData(Context ctx)
{
    Cursor cursor = ctx.getContentResolver().query(....);
}
 
Back
Top Bottom