loader 编写一个android,在Activity Android中实现LoaderCallbacks

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 {

@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 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 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 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

found: int,,MainActivity

reason: cannot infer type-variable(s) D

(argument mismatch; MainActivity cannot be converted to LoaderCallbacks)

where D is a type-variable:

D extends Object declared in method initLoader(int,Bundle,LoaderCallbacks)

解决方案

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().

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值