ListView中的Android数据
问题描述:
我正在开发此联系人应用程序。到目前为止,我已经生成了一个ListView女巫有联系人姓名和电话号码。当您点击联系人时,它会开始新的活动并显示联系人姓名和电话号码。ListView中的Android数据
我想要做的是ListView我只显示联系人姓名,当你点击列表中的联系人,然后活动开始,你可以看到名字和数字。
我想也许我可以在ListView中隐藏一些信息,但我没有发现任何好的东西。
那么有没有人有任何建议?
在此先感谢。
答
首先,仅查询联系人姓名和ID:
在你的清单,你必须声明
<uses-permission android:name="android.permission.READ_CONTACTS" />
一旦你得到了与接触,你必须光标通过,在一个CursorAdapter
private final static String[] FROM_COLUMNS = {
Build.VERSION.SDK_INT
>= Build.VERSION_CODES.HONEYCOMB ?
Contacts.DISPLAY_NAME_PRIMARY :
Contacts.DISPLAY_NAME
};
private final static int[] TO_IDS = {
android.R.id.text1
};
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
...
// Gets the ListView from the View list of the parent activity
mContactsList =
(ListView) getActivity().findViewById(R.layout.contact_list_view);
// Gets a CursorAdapter
mCursorAdapter = new SimpleCursorAdapter(
getActivity(),
R.layout.contact_list_item,
null,
FROM_COLUMNS, TO_IDS,
0);
// Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
// Prepare the loader. Either re-connect with an existing one,
// or start a new one.
getLoaderManager().initLoader(0, null, this);
}
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);
}
}
不确定你在问什么 - “当你点击联系人时,它开始新的活动并显示联系人姓名和电话号码。”与您点击列表中的联系人非常相似,然后活动开始,您可以看到姓名和号码。“ – 2014-09-24 22:00:26
okei对不起,我有点不清楚,当程序启动时,我的第一个内容是什么。它显示了联系人列表,我希望它只显示联系人的名字。但现在它显示了姓名和电话号码。 – user1032336 2014-09-24 22:12:57