android 读取电话本,并加载到list相关
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. */@Overridepublic 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 layoutmLinLayout.addView(mLstViw, params);setContentView(mLinLayout); listData = new ArrayList<Map<String, String>>(); // read contactsCursor 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 stubDisplayToast("选中第 " + Integer.toString(arg2 + 1) + "个");}}); mLstViw.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {// TODO Auto-generated method stubDisplayToast("滚动到" + 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();}}
更多相关文章
- Android(安卓)帧布局(FrameLayout)的理解
- 关于下拉框的一些应用
- Android(安卓)顶部状态栏覆盖到应用之上?
- Android——自定义自适应高度的GridView
- Android(安卓)sensor 大杂烩
- 关于android studio下的handler问题
- Android布局控件的继承关系
- Android(安卓)Dialog 应用
- 改变分隔条,基于数组的ListView