CursorLoader无法转换为Loader <Cursor>

时间:2019-10-29 10:56:42

标签: android android-loadermanager

我正在尝试在ProfileFragment片段中显示用户数据。 这样我实现了LoaderManager.LoaderCallbacks<Cursor> 但发生以下错误:

CursorLoader cannot be converted to Loader<Cursor>

我该如何解决?

我在Google上进行了搜索,但找不到适合我问题的正确解决方案。 有什么解决办法吗?

先谢谢您

ProfileFragment.java

package com.example.takeattendence;

import android.content.Context;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.loader.app.LoaderManager;
import androidx.loader.content.Loader;

import com.example.takeattendence.database.LoginContract;


public class ProfileFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>
{
    private static final int PET_LOADER = 0;
    LoginCursorAdapter mLoginCursorAdapter;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_profile,container,false);
        ListView listView = view.findViewById(R.id.profile_list);

        mLoginCursorAdapter = new LoginCursorAdapter(getActivity(),null);
        listView.setAdapter(mLoginCursorAdapter);
        getLoaderManager().initLoader(PET_LOADER,null,this);
        return view;

    }

    @NonNull
    @Override
    public Loader<Cursor> onCreateLoader(int id, @Nullable Bundle args) {
                String[] projection = {
                BaseColumns._ID,
                LoginContract.LoginEntry.COLUMN_FIRST_NAME,
                LoginContract.LoginEntry.COLUMN_PHONE_NUMBER,
                LoginContract.LoginEntry.COLUMN_EMAIL_ID,
                LoginContract.LoginEntry.COLUMN_PASSWORD,
                LoginContract.LoginEntry.COLUMN_POST,
                LoginContract.LoginEntry.COLUMN_GENDER
        };

        //error occurs at this line
        return new CursorLoader(getContext(),
                LoginContract.LoginEntry.CONTENT_URI,
                projection,
                null,
                null,
                null);
        return null;
    }


    @Override
    public void onLoadFinished(@NonNull Loader<Cursor> loader, Cursor data) {
        mLoginCursorAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(@NonNull Loader<Cursor> loader) {
        mLoginCursorAdapter.swapCursor(null);
    }
}

1 个答案:

答案 0 :(得分:0)

您导入android.content.CursorLoader,但必须导入androidx.loader.content.CursorLoader,因为您还使用了AndroidX(androidx.loader.content.Loader)中的加载程序。