【发布时间】:2013-08-14 11:52:19
【问题描述】:
很抱歉,如果这看起来像一百万次相同的问题...但谷歌搜索没有结果,只是一堆过时的教程使用 managedQuery 和其他已弃用的解决方案...
我浏览了android developer training for retrieving a contact list,但教程不完整,甚至下载示例代码也无济于事,因为示例代码用于更高级的联系人列表操作(搜索等)
无论如何,没有理由不应该有一个简单的解决方案,所以我希望有人能在这里回答,因为我确信这已经完成了一百万次,而且我确信还有几十个其他的刚开始的 android 开发人员会很感激这一点。
据我所知,我已按照教程进行操作,但没有显示任何联系人。我认为最重要的是TO_IDS 是一个指向android.R.id.text1 的整数数组。我很困惑这应该如何以某种方式提取一系列联系人姓名。
另外,我很困惑为什么当最终目标是显示列表视图时需要文本视图...在本教程中,我们有 mContactsList 这是一个列表视图...但是我们使用适配器填充列表视图指向R.layout.contact_list_item,它只是由TO_IDS(整数数组)填充的文本视图。
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);
我做错了什么,如何在列表视图中简单地显示联系人列表?
编辑:添加我的代码:
在我的片段类中:
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 中:
<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中:
<?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中
<?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:
我在contact_list_layout.xml 中输入了什么?这只是一个空的<LinearLayout>?教程中不清楚如何处理这个 xml。它说这个 XML 是片段,但如果它是片段,为什么我们在 contact_list_view.xml 中已经定义了一个 listview?
【问题讨论】:
-
FROM_COLUMNS, TO_IDS,将数据库列映射到文本视图 ID(在每个列表项中),请参阅SimpleCursorAdapter。有些东西需要加载光标数据才能使用它,而那不在你的代码 sn-p -
@zapl 我添加了一些代码来向您展示我的
FROM_COLUMNS和TO_IDS...我注意到我没有contact_list_layout.xml而我有@987654341 @...你能告诉我contact_list_layout.xml的结构吗?您可以查看我的 xml 文件吗?我有一种感觉,我只是想念一些东西,一旦我发现它会起作用。该教程没有显示contact_list_layout.xml是如何编写的,所以也许这就是我犯错的地方? -
@zapl 我在
mContactList.setAdapter(mCursorAdapter);得到一个空指针异常 -
@zapl 好的,我发现了我的一个问题。我的
mContactsList引用了我认为从未实例化的listview,我相信mContactsList也引用了尚未创建的布局。这导致了一个空指针...我假设我必须在我缺少的contact_list_layout.xml中创建它们...你知道我需要做什么吗?
标签: java android listview simplecursoradapter