7

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

    }
}
4

2 回答 2

3

Example on how to use changeCursor() with LoaderManager.LoaderCallbacks instead of 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);
}

if you want to use the swapCursor() available in API 11 for older API's, remember to import the CursorAdapter from the support library.

import android.support.v4.widget.CursorAdapter;
于 2013-06-07T11:52:45.617 回答
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. 要使用ListView(和其他一些小部件),您必须有一个基于 Long 的主键,我发现这真的很不方便,因为我已经有一个完全可以接受的 UUID 作为我的主键。

于 2013-04-30T13:38:40.413 回答