Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 读取与loader manager的联系人_Android - Fatal编程技术网

Android 读取与loader manager的联系人

Android 读取与loader manager的联系人,android,Android,我试图利用LoaderManager和CursorLoader读取联系人的电话号码。我能读懂联系人的姓名,但我不知道如何读懂电话号码。任何指导都将不胜感激 // These are the Contacts rows that we will retrieve. static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { Contacts._ID, Contacts.DISPLAY_NAME,

我试图利用LoaderManager和CursorLoader读取联系人的电话号码。我能读懂联系人的姓名,但我不知道如何读懂电话号码。任何指导都将不胜感激

// These are the Contacts rows that we will retrieve.
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        Contacts._ID, Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER };

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;

// If non-null, this is the current filter the user has provided.
String mCurFilter;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Give some text to display if there is no data. In a real
    // application this would come from a resource.
    setEmptyText("No phone numbers");

    // Create an empty adapter we will use to display the loaded data.
    mAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_list_item_2, null, new String[] {
                    Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER },
            new int[] { android.R.id.text1, android.R.id.text2 }, 0);

    setListAdapter(mAdapter);

    // Start out with a progress indicator.
    setListShown(false);

    // Prepare the loader. Either re-connect with an existing one,
    // or start a new one.
    getLoaderManager().initLoader(0, null, this);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {

    Log.i("FragmentComplexList", "Item clicked: " + id);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created. This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;
    if (mCurFilter != null) {
        baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
                Uri.encode(mCurFilter));
    } else {
        baseUri = Contacts.CONTENT_URI;
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
            + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
            + Contacts.DISPLAY_NAME + " != '' ))";
    return new CursorLoader(getActivity(), baseUri,
            CONTACTS_SUMMARY_PROJECTION, select, null,
            Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in. (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);

    // The list should now be shown.
    if (isResumed()) {
        setListShown(true);
    } else {
        setListShownNoAnimation(true);
    }
}

public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed. We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
}
//这些是我们将检索的联系人行。
静态最终字符串[]联系人\u摘要\u投影=新字符串[]{
联系人。\u ID,Contacts.DISPLAY\u NAME,
ContactsContract.CommonDataTypes.Phone.NUMBER};
//这是用于显示列表数据的适配器。
简单的适应;
//如果非空,则这是用户提供的当前筛选器。
串滤波器;
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//如果没有数据,则提供一些文本以显示。在实际
//应用程序这将来自一个资源。
setEmptyText(“无电话号码”);
//创建一个空适配器,用于显示加载的数据。
mAdapter=new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_2,null,新字符串[]{
Contacts.DISPLAY_NAME,Contacts contract.commonDataTypes.Phone.NUMBER},
新的int[]{android.R.id.text1,android.R.id.text2},0);
setListAdapter(mAdapter);
//从进度指示器开始。
SetListShowed(假);
//准备加载程序。或者重新连接现有加载程序,
//或者开始一个新的。
getLoaderManager().initLoader(0,null,this);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
Log.i(“FragmentComplexList”,“单击项:”+id);
}
公共加载器onCreateLoader(int-id,Bundle-args){
//当需要创建新加载程序时,将调用此函数
//示例只有一个加载程序,所以我们不关心ID。
//首先,选择要使用的基本URI,这取决于我们是否
//当前正在筛选。
uribaseuri;
if(mCurFilter!=null){
baseUri=Uri.withAppendedPath(Contacts.CONTENT\u FILTER\u Uri,
encode(mCurFilter));
}否则{
baseUri=Contacts.CONTENT\u URI;
}
//现在创建并返回一个游标加载程序,它将处理
//为正在显示的数据创建光标。
String select=“(“+Contacts.DISPLAY_NAME+”NOTNULL)和(”
+Contacts.HAS_PHONE_NUMBER+“=1)和(”
+Contacts.DISPLAY_NAME+“!=”);
返回新的游标装入器(getActivity(),baseUri,
联系人\u摘要\u投影,选择,空,
Contacts.DISPLAY_NAME+“核对本地化ASC”);
}
public void onLoadFinished(加载器、光标数据){
//在中交换新光标。(框架将负责关闭
//我们返回后,将返回旧光标。)
mAdapter.swapCursor(数据);
//现在应显示该列表。
如果(isResumed()){
SetListShowed(真);
}否则{
SetListShownAnimation(真);
}
}
公共void onLoaderReset(加载器){
//当提供给onLoadFinished()的最后一个游标时调用
//以上内容即将关闭。我们需要确保我们没有
//不再使用它。
mAdapter.swapCursor(空);
}

试试这个。。这对我有用

// This is the Adapter being used to display the list's data.
    SimpleCursorAdapter mAdapter;

    // If non-null, this is the current filter the user has provided.
    String mCurFilter;

    @Override public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Give some text to display if there is no data.  In a real
        // application this would come from a resource.
        setEmptyText("No phone numbers");

        // We have a menu item to show in action bar.
        setHasOptionsMenu(true);

        // Create an empty adapter we will use to display the loaded data.
        mAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_list_item_1, null,
                new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, },
                new int[] { android.R.id.text1}, 0);
        setListAdapter(mAdapter);

        // Start out with a progress indicator.
        setListShown(false);

        // Prepare the loader.  Either re-connect with an existing one,
        // or start a new one.
        getLoaderManager().initLoader(0, null, this);
    }

    @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Place an action bar item for searching.
        MenuItem item = menu.add("Search");
        item.setIcon(android.R.drawable.ic_menu_search);
        item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        View searchView = SearchViewCompat.newSearchView(getActivity());
        if (searchView != null) {
            SearchViewCompat.setOnQueryTextListener(searchView,
                    new OnQueryTextListenerCompat() {
                @Override
                public boolean onQueryTextChange(String newText) {
                    // Called when the action bar search text has changed.  Update
                    // the search filter, and restart the loader to do a new query
                    // with this filter.
                    mCurFilter = !TextUtils.isEmpty(newText) ? newText : null;
                    getLoaderManager().restartLoader(0, null, CursorLoaderListFragment.this);
                    return true;
                }
            });
            item.setActionView(searchView);
        }
    }

    @Override public void onListItemClick(ListView l, View v, int position, long id) {
        // Insert desired behavior here.
        Log.i("FragmentComplexList", "Item clicked: " + id);
    }

    // These are the Contacts rows that we will retrieve.
    static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
        ContactsContract.CommonDataKinds.Phone._ID,
        ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER,
    };

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        // This is called when a new Loader needs to be created.  This
        // sample only has one Loader, so we don't care about the ID.
        // First, pick the base URI to use depending on whether we are
        // currently filtering.
        Uri baseUri;
        if (mCurFilter != null) {
            baseUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(mCurFilter));
        } else {
            baseUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
        }

        // Now create and return a CursorLoader that will take care of
        // creating a Cursor for the data being displayed.
        String select = "((" + ContactsContract.Contacts.DISPLAY_NAME + " NOTNULL) AND ("
                + ContactsContract.Contacts.DISPLAY_NAME + " != '' )" + "AND ("+ContactsContract.Contacts.HAS_PHONE_NUMBER +" != '0'"+"))";

        return new CursorLoader(getActivity(), baseUri,
                CONTACTS_SUMMARY_PROJECTION, null, null,
                null + " COLLATE LOCALIZED ASC");
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);

        // The list should now be shown.
        if (isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished()
        // above is about to be closed.  We need to make sure we are no
        // longer using it.
        mAdapter.swapCursor(null);
    }
}
//这是用于显示列表数据的适配器。
简单的适应;
//如果非空,则这是用户提供的当前筛选器。
串滤波器;
@在ActivityCreated(Bundle savedInstanceState)上重写公共void{
super.onActivityCreated(savedInstanceState);
//如果没有数据,则提供一些文本以显示。在实际
//应用程序这将来自一个资源。
setEmptyText(“无电话号码”);
//我们有一个菜单项显示在行动栏。
设置选项菜单(真);
//创建一个空适配器,用于显示加载的数据。
mAdapter=new SimpleCursorAdapter(getActivity(),
android.R.layout.simple_list_item_1,null,
新字符串[]{ContactsContract.CommonDataTypes.Phone.NUMBER,},
新的int[]{android.R.id.text1},0);
setListAdapter(mAdapter);
//从进度指示器开始。
SetListShowed(假);
//准备加载程序。或者重新连接现有加载程序,
//或者开始一个新的。
getLoaderManager().initLoader(0,null,this);
}
@覆盖创建选项菜单(菜单菜单,菜单充气机){
//放置一个用于搜索的操作栏项。
MenuItem item=menu.add(“搜索”);
设置图标(android.R.drawable.ic\u菜单\u搜索);
item.setShowAsAction(菜单项始终显示为动作);
View searchView=SearchViewCompat.newSearchView(getActivity());
if(searchView!=null){
SearchViewCompat.setOnQueryTextListener(searchView,
新OnQueryTextListenerCompat(){
@凌驾
公共布尔onQueryTextChange(字符串newText){
//当操作栏搜索文本已更改时调用。更新
//搜索筛选器,然后重新启动加载程序以执行新查询
//用这个过滤器。
mCurFilter=!TextUtils.isEmpty(newText)?newText:null;
getLoaderManager().restartLoader(0,null,CursorLoaderListFragment.this);
返回true;
}
});
item.setActionView(searchView);
}
}
@重写公共void onListItemClick(列表视图l、视图v、整型位置、长id){
//在此处插入所需的行为。
Log.i(“FragmentComplexList”,“单击项:”+id);
}
//这些是我们将检索的联系人行。
静态最终字符串[]联系人\u摘要\u投影=新字符串[]{
Contacts contract.CommonDataTypes.Phone.\u ID,
contacts contract.CommonDat