Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What the appropriate replacer of deprecated " managedQuery"?

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...

like image 902
ilw Avatar asked Sep 30 '14 12:09

ilw


2 Answers

You can see this link : Deprecated ManagedQuery() issue

Cursor cursor = getContentResolver().query(contentUri, null, null, null, Contacts.DISPLAY_NAME);
like image 103
Harsh Parikh Avatar answered Dec 08 '22 06:12

Harsh Parikh


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);
}

}
like image 22
ilw Avatar answered Dec 08 '22 06:12

ilw