Android联系人 - 检索到的Android联系人,如何将它们放在一个简单的ListView中?
我的应用程序的一部分我需要一个列表,当选择该选项时显示所有联系人(包括电话号码)。Android联系人 - 检索到的Android联系人,如何将它们放在一个简单的ListView中?
这里是按下按钮时,这就是所谓的活动:
package com.example.prototype01;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
public class nominateContactsActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nominatecontactslayout);
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String contactName, contactTelNumber = "";
String contactID;
c.moveToFirst();
for (int i = 0; i < c.getCount(); i++) {
contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
Log.i("name ", contactName + " ");
Log.i("number ", contactTelNumber + " ");
c.moveToNext();
}
}
}
正如你所看到的,这个代码返回存储在手机上的所有联系人的姓名和电话号码。目前,这些只是回应logcat。我似乎无法解决如何在列表视图中列出这些项目,而只显示名称和编号。我已经跟随了几个教程无济于事,所以我很不情愿地寻求你的协助。我很不情愿地说,因为我确信这个问题已经被多次解答,我似乎无法将解决方案应用到我的代码中!
预先感谢您!
亲切的问候, Antwan
这里是为了适应你已经有一个办法。我添加了注释的一切之前,我改变了沿途讲解:
public class nominateContactsActivity extends Activity {
// Add a list to keep all the "name: number" strings
private List<String> mNameNumber = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nominatecontactslayout);
Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
String contactName, contactTelNumber = "";
String contactID;
// You only need to find these indices once
int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
int hasNumberIndex = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
// This is simpler than calling getCount() every iteration
while(c.moveToNext()) {
contactName = c.getString(nameIndex);
contactID = c.getString(idIndex);
// If this is an integer ask for an integer
if (c.getInt(hasNumberIndex)) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// Store the "name: number" string in our list
mNameNumber.add(contactName + ": " + contactTelNumber);
}
}
}
// Find the ListView, create the adapter, and bind them
ListView listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mNameNumber);
listView.setAdapter(adapter);
}
}
这里是我会做什么把联系人在ListView
在你的活动:
private ListView mContactsListView;
private ListContactItemAdapter mContactsListAdapter;
this.mContactsListAdapter = new ListContactItemAdapter(this, R.layout.contact_row);
// after doing setContentView, assuming you have defined a listview in your layout file
this.mContactsListView= (ListView) this.findViewById(R.id.contactsListView);
// You may want to create a custom adapter, which I wrote below for showing you example
this.mContactsListView.setAdapter(this.mContactsListAdapter);
for (/* each contact */) {
Contact contact = new Contact();
contact.name = contactName;
contact.number = contactNumber;
this.mContactsListAdapter.add(contact);
}
// In order to refresh your list and make data appear
this.mContactsListAdapter.notifyDataSetChanged();
在
当然我的例子你需要一个包含联系数据的模型对象:
public class Contact {
public String name;
public String number;
}
这可能是使用上面的联系类自定义适配器:
public class ListContactItemAdapter extends ArrayAdapter<Contact> {
private int mLineLayout;
private LayoutInflater mInflater;
public ListContactItemAdapter(Context pContext, int pLineLayout) {
super(pContext, pLineLayout);
this.mLineLayout = pLineLayout;
this.mInflater = (LayoutInflater) pContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
static class ViewHolder {
TextView contactName;
TextView contactNumber;
}
@Override
public View getView(int pPosition, View pView, ViewGroup pParent) {
ViewHolder holder;
if (pView == null) {
pView = this.mInflater.inflate(this.mLineLayout, null);
holder = new ViewHolder();
holder.contactName = (TextView) pView.findViewById(R.id.contactName);
holder.contactNumber = (TextView) pView.findViewById(R.id.contactNumber);
pView.setTag(holder);
} else {
holder = (ViewHolder) pView.getTag();
}
Contact contact = getItem(pPosition);
if (contact != null) {
holder.contactName.setText(contact.name);
holder.contactNumber.setText(contact.number);
}
return pView;
}
}
对于例如你需要的布局来定义列表视图的列。这可能是一个contact_row.xml的例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/contactName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:text="Contact name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/contactNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/contactName"
android:paddingLeft="6dp"
android:text="number"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
我没有尝试,但这应该工作。无论如何,我希望它能让你理解列表视图的基本功能。
非常感谢您的协助,我知道有一个完整的工作应用程序经过数小时的尝试和撕掉头发! 非常感谢你 – user1593869 2012-08-12 22:22:04
嗨在那里,经过一些测试后,我似乎错过了一些东西...... 当我向下滚动联系人列表时,勾选框变为不勾选,'新'顶部位置变为打勾 - 如果你看到我的意思,很难解释而不看! 你有什么建议吗? – user1593869 2013-02-11 14:54:49
项目在滚动时被回收,因此您的复选框(选中)随循环过程而移动。你应该关心你的勾号框的位置,跟踪检查的位置,类似的东西,并在getView中以编程方式管理勾号。或者如果你想改变你的方法通过使用CheckedTextView,也许这个答案我(很久以前)可以帮助你http://stackoverflow.com/questions/12028167/checkable-listview-cursor-data-independent-checkbox/12029688#12029688 – 2013-02-11 16:06:09
是不是推荐使用CursorLoaders,或者在这种情况下不适用? – aamit915 2012-08-12 22:07:02
@ aamit915 CursorLoaders已添加到API 11中,并且它们在某种程度上包含在向后兼容性库中。但是,如果作者只想在API 11之前的设备上运行它,则不是必需的。 – Sam 2012-08-12 22:11:30
好的谢谢澄清。 – aamit915 2012-08-12 23:41:03