创建CursorLoader时,我收到“swapCursor(Cursor)未定义”错误。我已经导入了android.support.v4(app.LoaderManager,app.LoaderManager.Loader,Content.CursorLoader,content.Loader)。不知道该如何解决该问题。请指教。

装载机:

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.app.ListFragment;
import android.support.v4.widget.CursorAdapter;



public class LoginList extends FragmentActivity implements OnClickListener,
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {

    private ListView loginList;
    private Button webLogin;
    private SimpleCursorAdapter adapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_listview);

        loginList = (ListView)findViewById(R.id.loginlist);
        loginList.setOnItemClickListener(this);

        webLogin = (Button)findViewById(R.id.button3);
        webLogin.setOnClickListener(this);



        //Specify fileds to display in the list
        String[] from = new String[] { ListProvider.COLUMN_NAME_SITE };

        //Bind fields to listview
        int[] to = new int[] {R.id.loginlist};

        // Create CursorAdapter and set it to display
        adapter = new SimpleCursorAdapter(this, R.layout.login_listview, null, from, to);

        loginList.setAdapter(adapter);

        getSupportLoaderManager().initLoader(0, null, this);
    }

    /*@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

    super.onViewCreated(view, savedInstanceState);
    setEmptyText(getResources().getString(string.app_name));
    rwgisterForContextMenu(getListView());
    setHasOptionsMenu(true);
        } */


    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        // TODO Auto-generated method stub

        Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

        Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);


        Cursor clickedObject = (Cursor)loginList.getItemAtPosition(arg2);

        Bundle loginBundle = new Bundle();
        loginBundle.putString("clickedWebSite",((LoginDetails) clickedObject).getsName());
        loginBundle.putString("clickedWebAddress",((LoginDetails) clickedObject).getwUrl());
        loginBundle.putString("clickedUserName",((LoginDetails) clickedObject).getuName());
        loginBundle.putString("clickedPassWord",((LoginDetails) clickedObject).getpWord());
        loginBundle.putString("clickedNotes",((LoginDetails) clickedObject).getlNotes());

        updateDeleteLoginInfo.putExtras(loginBundle);

        startActivityForResult(updateDeleteLoginInfo, 0);
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
        startActivity(webLoginIntent);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int ignored, final Bundle args) {
        return new CursorLoader(this, ListProvider.CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor); /* swapCursor error: The method swapCursor(Cursor) is undefined for the type SimpleCursorAdapter */


    }

    @Override
    public void onLoaderReset (Loader<Cursor> loader) {
        adapter.swapCursor(null); /* swapCursor error: The method swapCursor(Cursor) is undefined for the type SimpleCursorAdapter */

    }
}

最佳答案

有关如何将changeCursor()与LoaderManager.LoaderCallbacks而不是swapCursor()一起使用的示例。

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    return new CursorLoader(this, DB_table.CONTENT_URI, DB_table.PROJECTION,null,null,null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    mAdapter.changeCursor(data);
}

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

如果您想将API 11中可用的swapCursor()用于较旧的API,请记住从支持库中导入CursorAdapter。
import android.support.v4.widget.CursorAdapter;

07-27 17:43