swapCursor未定义错误

时间:2013-03-30 22:38:07

标签: android listview cursor android-arrayadapter simplecursoradapter

创建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 */

    }
}

2 个答案:

答案 0 :(得分:2)

有关如何将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中提供的旧版API的swapCursor(),请记住从支持库中导入CursorAdapter。

import android.support.v4.widget.CursorAdapter;

答案 1 :(得分:0)

onCreate()方法中,初始化适配器,将ListView作为第二个参数传递。这是不正确的,它应该是为列表中的每个项目实例化的布局ID:

adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, null, from, to);

除此之外,您的代码对我来说很好。我将它与使用ListFragment的我的工作代码进行了比较,因此我没有实例化UI而是覆盖了onActivityCreated(Bundle)方法。

我确实提供了一个投影来减少我ContentProvider从数据库中提取的数据量,但传递null应该不是问题。

但请检查您是否在ContentProvier中提供了“_id”主键列。要使用ListView(和其他一些小部件),你必须有一个基于Long的主键,我发现这非常不方便,因为我已经有一个完全可以接受的UUID作为我的主键。