gpt4 book ai didi

java - Android 联系人 - 检索 Android 联系人,如何将它们放在简单的 ListView 中?

转载 作者:搜寻专家 更新时间:2023-11-01 09:05:48 28 4
gpt4 key购买 nike

对于我的应用程序的一部分,我需要在选择该选项时显示所有联系人的列表(带有电话号码)。

这是按下按钮时调用的 Activity :

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。我似乎无法弄清楚如何在 ListView 中列出这些项目,而只显示名称和编号。我遵循了几个教程但无济于事,因此我不情愿地寻求您的帮助。我不情愿地说,因为我确定这个问题已经回答了很多次,我似乎无法将解决方案应用到我的代码中!

提前致谢!

亲切的问候,安特万

最佳答案

这是将联系人放入 listView 中的方法

在您的 Activity 中:

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;
}

对于该示例,您需要一个布局来定义 ListView 的行。这可能是一个 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>

我没试过,但这应该行得通。无论如何,我希望它能让您了解 ListView 的基本工作原理。

关于java - Android 联系人 - 检索 Android 联系人,如何将它们放在简单的 ListView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11926135/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com