What the appropriate replacer of deprecated " managedQuery"?
Asked Answered
R

2

10

Android documentation said: This method was deprecated in API level 11.

This is code:

class GridViewActivity_ extends     Activity
  {
    @Override
    protected void onCreate(Bundle  savedInstanceState)
    {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);

    GridView gv = (GridView)findViewById(R.id.gridview);

    Cursor c = managedQuery(Contacts.CONTENT_URI, 
            null, null, null, Contacts.DISPLAY_NAME);

    String[] cols = new String[]{Contacts.DISPLAY_NAME};
    int[]   views = new int[]   {android.R.id.text1};

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,
            c, cols, views);
    gv.setAdapter(adapter);
}
 }

How replace this code , not deprecated code?

For activity, not fragment...

Raguelragweed answered 30/9, 2014 at 12:22 Comment(1)
cursor loader developer.android.com/reference/android/content/…Foozle
R
3

According to this great tutorial :

public class GridViewActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>
{
    private SimpleCursorAdapter mAdapter;

    @Override
public Loader<Cursor> onCreateLoader(int p1, Bundle p2)
{
    return new CursorLoader(this, Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME);
}

@Override
public void onLoadFinished(Loader<Cursor> p1, Cursor cursor)
{
    mAdapter.swapCursor(cursor);
}

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


@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);      
    GridView gv = (GridView)findViewById(R.id.gridview);
    String[] cols = new String[]{Contacts.DISPLAY_NAME};
    int[] views = new int[]{android.R.id.text1};
    mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, cols,views, 0);
    gv.setAdapter(mAdapter);
    getSupportLoaderManager().initLoader(0, null, this);
}

}
Raguelragweed answered 30/9, 2014 at 14:46 Comment(1)
Seems getSupportLoaderManager is also deprecated (IDE says so). How come? What should be used instead?Jame
G
20

You can see this link : Deprecated ManagedQuery() issue

Cursor cursor = getContentResolver().query(contentUri, null, null, null, Contacts.DISPLAY_NAME);
Gaius answered 30/9, 2014 at 12:33 Comment(1)
Just a thing to note. This requires 4 parameters, managedQuery required only 3. I had been ignoring one of the null !Eagan
R
3

According to this great tutorial :

public class GridViewActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>
{
    private SimpleCursorAdapter mAdapter;

    @Override
public Loader<Cursor> onCreateLoader(int p1, Bundle p2)
{
    return new CursorLoader(this, Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME);
}

@Override
public void onLoadFinished(Loader<Cursor> p1, Cursor cursor)
{
    mAdapter.swapCursor(cursor);
}

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


@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO: Implement this method
    super.onCreate(savedInstanceState);
    setContentView(R.layout.gridview);      
    GridView gv = (GridView)findViewById(R.id.gridview);
    String[] cols = new String[]{Contacts.DISPLAY_NAME};
    int[] views = new int[]{android.R.id.text1};
    mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, cols,views, 0);
    gv.setAdapter(mAdapter);
    getSupportLoaderManager().initLoader(0, null, this);
}

}
Raguelragweed answered 30/9, 2014 at 14:46 Comment(1)
Seems getSupportLoaderManager is also deprecated (IDE says so). How come? What should be used instead?Jame

© 2022 - 2024 — McMap. All rights reserved.