本文介绍了弃用的“"的适当替代者是什么? managedQuery"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android文档说:此方法在API级别11中已弃用.

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

这是代码:

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

推荐答案

根据此出色的教程:

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

}

这篇关于弃用的“"的适当替代者是什么? managedQuery"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 17:24