基于Android的通讯录代码

时间: 2024-01-08 14:52:55 浏览: 37
以下是一个简单的基于 Android 平台的通讯录应用程序的代码示例: MainActivity.java ``` import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private ListView listView; private ArrayList<Contact> contacts = new ArrayList<>(); private ContactAdapter contactAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = findViewById(R.id.listView); Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); contacts.add(new Contact(name, phoneNumber)); } cursor.close(); contactAdapter = new ContactAdapter(this, contacts); listView.setAdapter(contactAdapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this, ContactActivity.class); intent.putExtra("name", contacts.get(position).getName()); intent.putExtra("phoneNumber", contacts.get(position).getPhoneNumber()); startActivity(intent); } }); } } ``` Contact.java ``` public class Contact { private String name; private String phoneNumber; public Contact(String name, String phoneNumber) { this.name = name; this.phoneNumber = phoneNumber; } public String getName() { return name; } public String getPhoneNumber() { return phoneNumber; } } ``` ContactAdapter.java ``` import android.content.Context; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import java.util.ArrayList; public class ContactAdapter extends ArrayAdapter<Contact> { private Context context; private ArrayList<Contact> contacts; public ContactAdapter(@NonNull Context context, ArrayList<Contact> contacts) { super(context, 0, contacts); this.context = context; this.contacts = contacts; } @NonNull @Override public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { View listItemView = convertView; if (listItemView == null) { listItemView = LayoutInflater.from(context).inflate(R.layout.contact_item, parent, false); } Contact currentContact = contacts.get(position); TextView nameTextView = listItemView.findViewById(R.id.nameTextView); nameTextView.setText(currentContact.getName()); TextView phoneNumberTextView = listItemView.findViewById(R.id.phoneNumberTextView); phoneNumberTextView.setText(currentContact.getPhoneNumber()); return listItemView; } } ``` activity_main.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" tools:context=".MainActivity"> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout> ``` contact_item.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp"> <TextView android:id="@+id/nameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/phoneNumberTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/nameTextView" android:textSize="14sp" /> </RelativeLayout> ``` ContactActivity.java ``` import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; public class ContactActivity extends AppCompatActivity { private TextView nameTextView; private TextView phoneNumberTextView; private Button callButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); nameTextView = findViewById(R.id.nameTextView); phoneNumberTextView = findViewById(R.id.phoneNumberTextView); callButton = findViewById(R.id.callButton); Intent intent = getIntent(); String name = intent.getStringExtra("name"); String phoneNumber = intent.getStringExtra("phoneNumber"); nameTextView.setText(name); phoneNumberTextView.setText(phoneNumber); callButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String phoneNumber = phoneNumberTextView.getText().toString(); Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + phoneNumber)); startActivity(intent); } }); } } ``` activity_contact.xml ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="16dp" tools:context=".ContactActivity"> <TextView android:id="@+id/nameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/phoneNumberTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/nameTextView" android:textSize="14sp" /> <Button android:id="@+id/callButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/phoneNumberTextView" android:text="Call" /> </RelativeLayout> ```

相关推荐

最新推荐

recommend-type

基于Android手机通讯录的设计与实现

一篇很好的关于Android的本科毕业论文《基于android手机通讯录的设计与实现毕业论文》转自百度
recommend-type

Android自定义View实现通讯录字母索引(仿微信通讯录)

本文主要介绍了Android自定义View实现通讯录字母索引(仿微信通讯录)的实现步骤与方法,具有很好的参考价值,下面跟着小编一起来看下吧
recommend-type

C语言基于哈希表实现通讯录

主要为大家详细介绍了C语言基于哈希表实现通讯录,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

基于Android手机通讯录的设计与实现_2018.doc

基于Android手机通讯录的设计与实现,基于Android手机通讯录的设计与实现
recommend-type

Android获取手机通讯录、sim卡联系人及调用拨号界面方法

主要介绍了Android获取手机通讯录、sim卡联系人及调用拨号界面方法,本文分别给出实现代码实现获取通讯录和sim卡的联系人,以及权限配置和调用系统拨打电话的界面的实现代码,需要的朋友可以参考下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。