如何显示联系人在Android的列表视图为Android API 11+ [英] how to display contacts in a listview in Android for Android api 11+
问题描述
我很抱歉,如果这看起来像同样的问题一百万次...但谷歌搜索这提供了没有结果,只是一堆过时的教程,使用 managedQuery
等德precated解决方案...
I'm sorry if this looks like the same question a million times...but a google search for this provides no results, just a bunch of outdated tutorials using managedQuery
and other deprecated solutions...
我通过 Android开发培训去检索联系人列表,但教程是不完整的,甚至下载示例code没有帮助,因为样品code是更先进的联系人列表操作(搜索,等等。)
I went through the android developer training for retrieving a contact list, but the tutorial is incomplete and even downloading the sample code doesn't help because the sample code is for more advanced contact list manipulation (search, etc.)
在任何情况下,没有任何理由不应该有一个简单的解决这个所以我希望有人能在这里回答,因为我敢肯定,这项工作已经完成一百万次,我敢肯定,许多其他的开始Android的开发者AP preciate这一点。
In any case, there is no reason why there should not be a simple solution to this so I'm hoping someone can answer here because I'm sure this has been done a million times and I'm sure dozens of other beginning android developers would appreciate this.
我按照教程,尽我所知,没有联系人出现。我认为最重要的事情是, TO_IDS
是一个整数阵列指向 android.R.id.text1
。我很困惑怎么说应该以某种方式拉的联系人姓名的数组。
I have followed the tutorial to the best of my knowledge by no contacts show up. I think the biggest thing is that the TO_IDS
is an integer array that points to android.R.id.text1
. I'm confused how that is supposed to somehow pull an array of contact names.
另外,我很困惑,为什么一个TextView在需要时最终目标是显示一个列表视图...在本教程中,我们有mContactsList这是一个列表视图...但是我们填充列表视图的适配器指向 R.layout.contact_list_item
这是刚刚textviews居住着TO_IDS,整数数组。
Also, I'm confused why a textview is needed when the end goal is to display a listview...In the tutorial, we have mContactsList which is a list view...But we populate the list view with an adapter pointing to R.layout.contact_list_item
which is just textviews populated by TO_IDS, an array of integers.
mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contact_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
mContactList.setAdapter(mCursorAdapter);
我在做什么错了,我怎么只显示在列表视图中的联系人列表?
What am I doing wrong and how do I simply display the contact list in a listview?
编辑:在我的code加入:
adding in my code:
在我的片段类:
public class MyFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Cursor>{
private static final String[] FROM_COLUMNS = {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO_IDS = {android.R.id.text1};
ListView mContactList;
private SimpleCursorAdapter mCursorAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.contact_list_view,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mContactsList = (ListView) getActivity().findViewById(R.layout.contact_list_view);
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contact_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
mContactList.setAdapter(mCursorAdapter);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
return null;
}
@Override
public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
}
@Override
public void onLoaderReset(Loader<Cursor> cursorLoader) {
}
}
在我activity_main.xml:
in my activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id ="@+id/contactListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="preamble.myapp.MyFragment"/>
</LinearLayout>
在我contact_list_view的xml:
in my contact_list_view xml :
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
在我contact_list_item XML
In my contact_list_item xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
终于为contact_list_layout XML:
finally for contact_list_layout xml:
我怎么放在 contact_list_layout.xml
?这只是一个空的&LT;的LinearLayout&GT;
?目前尚不清楚如何此XML处理的教程。它说,此XML片段,但如果它的片段,为什么我们定义了一个列表视图
已经在 contact_list_view.xml
?
What do I put in the contact_list_layout.xml
? Is this just an empty <LinearLayout>
? It's not clear in the tutorial how this xml is handled. It says this XML is the fragment, but if it's the fragment, why did we define a listview
already in the contact_list_view.xml
?
推荐答案
小剥离下来的例子在一个的ListView
显示联系人的名字。
在片段
扩展 ListFragment
其中有一个默认的布局。你并不需要指定你自己的。列表项的布局也取自Android的默认布局( android.R.layout.simple_list_item_1
),这是每件简单的文本一行。
Small stripped down example for displaying the contacts name in a ListView
.
Below Fragment
extends ListFragment
which has a default layout. You don't need to specify your own. The layout for list items is also taken from Android's default layouts (android.R.layout.simple_list_item_1
) which is a simple single line of text per item.
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.Contacts;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> {
private CursorAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// create adapter once
Context context = getActivity();
int layout = android.R.layout.simple_list_item_1;
Cursor c = null; // there is no cursor yet
int flags = 0; // no auto-requery! Loader requeries.
mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// each time we are started use our listadapter
setListAdapter(mAdapter);
// and tell loader manager to start loading
getLoaderManager().initLoader(0, null, this);
}
// columns requested from the database
private static final String[] PROJECTION = {
Contacts._ID, // _ID is always required
Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
};
// and name should be displayed in the text1 textview in item layout
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// load from the "Contacts table"
Uri contentUri = Contacts.CONTENT_URI;
// no sub-selection, no sort order, simply every row
// projection says we want just the _id and the name column
return new CursorLoader(getActivity(),
contentUri,
PROJECTION,
null,
null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Once cursor is loaded, give it to adapter
mAdapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
// on reset take any old cursor away
mAdapter.swapCursor(null);
}
}
这篇关于如何显示联系人在Android的列表视图为Android API 11+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!