JosonChou 2012-11-13
1.加入权限
<!-- 允许读取电话号码信息 --> <uses-permission android:name="android.permission.READ_CONTACTS"/>
2.Activity
import android.app.ListActivity; import android.database.Cursor; import android.os.Bundle; import android.provider.Contacts.Phones; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; @SuppressWarnings("deprecation") public class Content extends ListActivity { private Cursor cursor ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // data数据 cursor = getContentResolver().query(Phones.CONTENT_URI, null, null, null, null); startManagingCursor(cursor); String[] get = new String[] { Phones.NAME, Phones.NUMBER }; int[] put = new int[] { R.id.usr_name, R.id.usr_mobile }; ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.user, cursor, get, put); setListAdapter(adapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); if(cursor.moveToPosition(position)){ StringBuilder sb = new StringBuilder() ; sb.append("名字:") .append(cursor.getString(cursor.getColumnIndex(Phones.NAME))).append("\n") .append("电话号码:").append(cursor.getString(cursor.getColumnIndex(Phones.NUMBER))); Toast.makeText(this,sb.toString(), Toast.LENGTH_SHORT).show() ; } ; } }
3.布局
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#770000ff" android:orientation="vertical" > <LinearLayout android:id="@+id/listLinearLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:scrollbars="vertical" > </ListView> </LinearLayout> </LinearLayout>
user.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="match_parent" android:background="#770000ff" android:orientation="vertical" > <TextView android:id="@+id/usr_name" android:layout_width="180dip" android:layout_height="30dip" android:singleLine="true" android:textSize="10pt" /> <TextView android:id="@+id/usr_mobile" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_toRightOf="@id/usr_name" android:textSize="10pt" /> </RelativeLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.fengso.fighter.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8"/> <!-- 允许读取电话号码信息 --> <uses-permission android:name="android.permission.READ_CONTACTS"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Content" android:label="@string/title_activity_content" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>