androidandroid-loadermanager

Implementing LoaderCallbacks in Activity Android


I'm new in android. I want to get contacts data using loaderCallbacks I write some code but here is some problem I don't know why this happen Can you please check it.

package com.example.arfan.myfirstapp;

import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportLoaderManager().initLoader(1, null, this); // ERROR IS HERE in 3rd Argument
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri CONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        return new CursorLoader(this, CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        Log.i("INFO","This is working");
        while(data.moveToNext()){
            Log.i("Cantacts : ",data.getString(data.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }
}

There is some kind of problem in this argument. If I remove this line then there is no error occur and also no result I found. I read this code from a website how to get contacts data in android you can this this site right here Go to Site

Can you please let me know what is the problem and how can I fix it. Thanks.

UPDATE: console showing this.

Error:(25, 34) error: method initLoader in class LoaderManager cannot be applied to given types;
required: int,Bundle,LoaderCallbacks<D>
found: int,<null>,MainActivity
reason: cannot infer type-variable(s) D
(argument mismatch; MainActivity cannot be converted to LoaderCallbacks<D>)
where D is a type-variable:
D extends Object declared in method <D>initLoader(int,Bundle,LoaderCallbacks<D>)

Solution

  • Since you're using AppCompatActivity and calling getSupportLoaderManager(), i suppose you would like to use the support library implementation of Loader as well.

    If so, change the following of your import statements:

    import android.app.LoaderManager;
    import android.content.CursorLoader;
    import android.content.Loader;
    

    To this:

    import android.support.v4.app.LoaderManager;
    import android.support.v4.content.CursorLoader;
    import android.support.v4.content.Loader;
    

    If you do not, call getLoaderManager() instead of getSupportLoaderManager().