在Activity Android中实现LoaderCallbacks [英] Implementing LoaderCallbacks in Activity Android

查看:250
本文介绍了在Activity Android中实现LoaderCallbacks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手.我想使用loaderCallbacks获取contacts数据 我写了一些代码,但是这里有一些问题,我不知道为什么会发生 你能检查一下吗?

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) {

    }
}

this参数中存在某种问题.如果删除此行,则不会发生错误,也不会找到结果.我从 网站如何在android中获取联系人数据,您可以在此访问此网站 转到网站

能否让我知道问题出在哪里以及如何解决. 谢谢.

更新: 控制台显示此内容.

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

解决方案

由于您正在使用AppCompatActivity并调用getSupportLoaderManager(),因此我想您也希望使用Loader的支持库实现./p>

如果是,请更改以下import语句:

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

对此:

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

如果不这样做,请呼叫getLoaderManager()而不是getSupportLoaderManager().

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

解决方案

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

这篇关于在Activity Android中实现LoaderCallbacks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆