【问题标题】:CursorLoader cannot be converted to Loader<Cursor>CursorLoader 无法转换为 Loader<Cursor>
【发布时间】:2019-10-29 10:56:42
【问题描述】:

我正在尝试在ProfileFragment Fragment 中显示用户数据。 所以我实现了LoaderManager.LoaderCallbacks&lt;Cursor&gt; 但出现以下错误:

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

【问题讨论】:

  • 您正在使用androidx.loader.content.Loader,它不是android.content.CursorLoader 的超类。您必须使用 androidx 的 CursorLoader: developer.android.com/reference/androidx/loader/content/… 版本才能使它们兼容
  • 好的,那我可以用什么?
  • You have to use androidx's version of CursorLoader
  • 你会写吗?
  • 尝试导入androidx.loader.content.CursorLoader 而不是android.content.CursorLoader

标签: android android-loadermanager


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 2013-04-04
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多