首页 > 代码库 > Android学习之适配器SimpleCursorAdapter

Android学习之适配器SimpleCursorAdapter

三.   SimpleCursorAdapter与SimpleAdapter用法相近。只是将List对象换成了Cursor对象。而且SimpleCursorAdapter类构造方法的第四个参数from表示Cursor对象中的字段,而SimpleAdapter类构造方法的第四个参数from表示Map对象中的key.

这个实例主要是查询通讯录,实现联系人拨号实例:

1.java代码:

 1 package com.example.simplecursoradapter; 2  3 import android.app.Activity; 4 import android.content.Intent; 5 import android.database.Cursor; 6 import android.database.CursorWrapper; 7 import android.graphics.Color; 8 import android.net.Uri; 9 import android.os.Bundle;10 import android.provider.Contacts.People;11 import android.telephony.PhoneNumberUtils;12 import android.util.Log;13 import android.view.View;14 import android.widget.AdapterView;15 import android.widget.LinearLayout;16 import android.widget.ListAdapter;17 import android.widget.ListView;18 import android.widget.SimpleCursorAdapter;19 20 21 public class MainActivity extends Activity {22     private static final String TAG = "MainActivity";23     ListView listView;24     ListAdapter adapter;25 26     @Override27     public void onCreate(Bundle savedInstanceState) {28         super.onCreate(savedInstanceState);29 30         LinearLayout linearLayout = new LinearLayout(this);31         linearLayout.setOrientation(LinearLayout.VERTICAL);32 33         LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(34                 LinearLayout.LayoutParams.FILL_PARENT,35                 LinearLayout.LayoutParams.WRAP_CONTENT);36 37         listView = new ListView(this);38         linearLayout.addView(listView, param);39         this.setContentView(linearLayout);40 41         // 从数据库获取联系人姓名和电话号码42         Cursor cur = this.getContentResolver().query(People.CONTENT_URI, null,43                 null, null, null);44         adapter = new SimpleCursorAdapter(this,45                 android.R.layout.simple_list_item_2, cur, new String[] {46                         People.NAME, People.NUMBER }, new int[] {47                         android.R.id.text1, android.R.id.text2 });48         this.startManagingCursor(cur);49         listView.setAdapter(adapter);50 51         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {52 53             public void onItemClick(AdapterView<?> arg0, View arg1,54                     int position, long arg3) {55 56                 // names=((CursorWrapper)listView.getItemAtPosition(position)).getColumnNames();57                 // 从指针的封装类中获得选中项的电话号码并拨号58                 CursorWrapper wrapper = (CursorWrapper) listView59                         .getItemAtPosition(position);60                 int columnIndex = wrapper.getColumnIndex(People.NUMBER);61                 if (!wrapper.isNull(columnIndex)) {62                     String number = wrapper.getString(columnIndex);63                     Log.d(TAG, "number=" + number);64                     // 判断电话号码的有效性65                     if (PhoneNumberUtils.isGlobalPhoneNumber(number)) {66                         Intent intent = new Intent(Intent.ACTION_DIAL, Uri67                                 .parse("tel://" + number));68                         startActivity(intent);69                     }70                 }71             }72         });73     }74 }

记得添加权限:

<!-- 点击拨号时,询问调用默认的程序还是调用本程序拨号 -->            <intent-filter>                 <action android:name="android.Intent.Action.CALL_BUTTON" />     <category android:name="android.Intent.Category.DEFAULT" />        </intent-filter><uses-permission android:name="android.permission.READ_CONTACTS" />

 

Android学习之适配器SimpleCursorAdapter