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     @Override
27     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) listView
59                         .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, Uri
67                                 .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" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM