使用ListView显示联系人的信息

本文介绍了一个基于Android的联系人列表应用程序的实现细节。该应用使用ListView组件展示从设备通讯录获取的联系人信息,包括姓名和电话号码。通过自定义适配器SimpleAdapter将数据绑定到list_item布局,实现了滚动选择和点击事件的监听。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

activity类:

public class MainActivity extends Activity {

private String TAG = "MainActivity";
private LinearLayout layout = null;
private ListView list = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
// layout.setBackgroundColor(Color.BLACK);
list = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
// list.setBackgroundColor(Color.RED);
layout.addView(list, param);
setContentView(layout);
list.setAdapter(getAdapter());
list.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
displayToast("滚动到了第"+Long.toString(arg0.getSelectedItemId())+"项");
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
//没有选中
displayToast("没有选中");
}
});
list.setOnItemClickListener(new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
displayToast("选中了第"+Integer.toString(arg2+1)+"项");
}
});
}

public ListAdapter getAdapter(){
List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while(cursor.moveToNext()){
HashMap<String,String> map = new HashMap<String,String>();
String name = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
int phoneCount = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = null;
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones.moveToFirst()) {
num = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
map.put("name", name);
map.put("num", num);
list.add(map);
}
Log.e(TAG,list.toString());
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.list_item,new String[]{"name","num"},new int[]{R.id.text01,R.id.text02});
return adapter;

}

public void displayToast(String string){
Toast.makeText(this, string, Toast.LENGTH_SHORT).show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

list_item.XML

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >


<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"
/>

<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="num"
/>
</LinearLayout>

转载于:https://www.cnblogs.com/Smart-Du/p/4301977.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值