a:
说明,使用ListView,需重写响应滚动和选择的监听器
在AndroidManifest.xml中加上申请权限的语句:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
主程序如下:
public class MyListView extends Activity {
private LinearLayout m_LinearLayout;
private ListView m_ListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//设定布局样式
m_LinearLayout=new LinearLayout(this);
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(Color.BLACK);
//初始化ListView
m_ListView=new ListView(this);
m_ListView.setBackgroundColor(Color.BLACK);
//布局参数
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
m_LinearLayout.addView(m_ListView);
//将m_LinearLayout布局放到当前的页面中显示
setContentView(m_LinearLayout);
//使用游标访问SQLite中的联系人
Cursor cur=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
//访问数据库的适配器
ListAdapter adpt=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,cur,new String[] {PhoneLookup.DISPLAY_NAME},new int[] {android.R.id.text1});
m_ListView.setAdapter(adpt);
//重写监听滚动的函数
m_ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
DisplayToast("滚动到第"+Long.toString(arg0.getSelectedItemId())+"项");
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
);
//重写监听选中的函数
m_ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
DisplayToast("选中了第"+Integer.toString(arg2+1)+"项");
}
});
}
private void DisplayToast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}
b:
public class Activity01 extends Activity {
LinearLayout mLinLayout;
ListView mLstViw;
ArrayList<Map<String, String>> listData;
static final String NAME = "name";
static final String NUMBER = "number";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinLayout = new LinearLayout(this);
mLinLayout.setOrientation(LinearLayout.VERTICAL);
mLinLayout.setBackgroundColor(Color.BLACK);
mLstViw = new ListView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mLstViw.setBackgroundColor(Color.BLACK);
// add the list view to layout
mLinLayout.addView(mLstViw, params);
setContentView(mLinLayout);
listData = new ArrayList<Map<String, String>>();
// read contacts
Cursor cur = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
startManagingCursor(cur);
while (cur.moveToNext()) {
Map<String, String> mp = new HashMap<String, String>();
long id = cur.getLong(cur.getColumnIndex("_id"));
Cursor pcur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
+ Long.toString(id), null, null);
// 处理多个号码的情况
String phoneNumbers = "";
while (pcur.moveToNext()) {
String strPhoneNumber = pcur
.getString(pcur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phoneNumbers += strPhoneNumber + ":";
}
phoneNumbers += "\n";
pcur.close();
String name = cur.getString(cur.getColumnIndex("display_name"));
mp.put(NAME, name);
mp.put(NUMBER, phoneNumbers);
listData.add(mp);
}
cur.close();
// 建立一个适配器去查询数据
ListAdapter adapter = new SimpleAdapter(this, listData,
android.R.layout.simple_list_item_2, new String[] { NAME,
NUMBER }, new int[] { android.R.id.text1,
android.R.id.text2 });
mLstViw.setAdapter(adapter);
mLstViw.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
DisplayToast("选中第 " + Integer.toString(arg2 + 1) + "个");
}
});
mLstViw.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
DisplayToast("滚动到" + Long.toString(arg0.getSelectedItemId())
+ "行");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void DisplayToast(String str) {
Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
}
}