I am trying to display user data in ProfileFragment Fragment.
So that I implemented LoaderManager.LoaderCallbacks<Cursor>
but following error occurs:
CursorLoader cannot be converted to Loader<Cursor>
How can I solve it?
I searched on Google but couldn't find the correct solution for my problem. Any solution for this?
Thank You in Advance.
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);
}
}
You import
android.content.CursorLoader, but you have to importandroidx.loader.content.CursorLoaderinstead, because you're also using the Loader from AndroidX (androidx.loader.content.Loader).