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>