对于我的部分申请,我需要在选择该选项时显示所有联系人(包括电话号码)的列表。
以下是按下按钮时调用的活动:
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
答案 0 :(得分:2)
以下是我将联系人放入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;
}
这可能是您使用上面的Contact类的自定义适配器:
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>
我没试过,但这应该有效。无论如何,我希望它能让你理解listviews基本上是如何工作的。
答案 1 :(得分:1)
这是一种适应你已有的方法。我在改变之前添加了评论,以便解释:
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);
}
}