Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android開發獲取聯系人到ListView中

Android開發獲取聯系人到ListView中

編輯:關於android開發

  參考代碼:

  import android.app.Activity;
  import android.app.PendingIntent;
  import android.content.ContentUris;
  import android.content.Intent;
  import android.database.Cursor;
  import android.database.CursorWrapper;
  import android.graphics.Color;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.Contacts.People;
  import android.telephony.PhoneNumberUtils;
  import android.telephony.gsm.SmsManager;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.LinearLayout;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleCursorAdapter;
  import android.widget.Toast;

  public class App extends Activity {
   private static final String TAG="App";
   ListView listView;
   ListAdapter adapter; //聲明一個適配器名稱
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
         // setContentView(R.layout.main);
          LinearLayout linearLayout=new LinearLayout(this);//實例化linearLayout,獲得其對象
          linearLayout.setOrientation(LinearLayout.VERTICAL);//設置布局方式,這裡面是垂直分布
          linearLayout.setBackgroundColor(Color.BLACK);//設置背景顏色
          LinearLayout.LayoutParams param  =
          new LinearLayout.LayoutParams

  (LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);//配置layout的長和寬  鏈接顯示LayoutParams的用法
       
          listView=new ListView(this);
          listView.setBackgroundColor(Color.BLACK);
       
          linearLayout.addView(listView,param);//動態添加View
       
          this.setContentView(linearLayout);
       
          //從數據庫獲取聯系人姓名和電話號碼
          Cursor cur=this.getContentResolver().query(People.CONTENT_URI,null, null,null,null);
          adapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cur,new String[]{People.NAME,People.NUMBER},new int[]{android.R.id.text1,android.R.id.text2});
          //SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) 構造函數參數
        this.startManagingCursor(cur);
          listView.setAdapter(adapter);
          //listView.setEmptyView(findViewById(R.id.empty));
       
          listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
     public void onItemSelected(AdapterView<?> arg0, View arg1,
       int arg2, long arg3) {
      // TODO Auto-generated method stub
      //openToast("滾動到:"+arg0.getSelectedItemId());
      //短信發送
      // PendingIntent pi = PendingIntent.getActivity(App.this,0,new Intent

  (App.this,App.class),0);
      // SmsManager sms = SmsManager.getDefault(); 
     //  sms.sendTextMessage("5554", null, "message", pi, null);
     }
     public void onNothingSelected(AdapterView<?> arg0) {
      // TODO Auto-generated method stub
   
     }
        
          });
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
     public void onItemClick(AdapterView<?> arg0, View arg1, int position,
       long arg3) {
      // TODO Auto-generated method stub
  //    String[] names=((CursorWrapper)listView.getItemAtPosition(position)).getColumnNames

  ();
      //從指針的封裝類中獲得選中項的電話號碼並撥號
      CursorWrapper wrapper=(CursorWrapper)listView.getItemAtPosition(position);//返回值是Object類需要向下轉型成CursorWrapper類型
      int columnIndex=wrapper.getColumnIndex(People.NUMBER);//返回從0開始的索引,如果列名不存在將返回-1
      if(!wrapper.isNull(columnIndex)){
       String number=wrapper.getString(columnIndex);
       Log.d(TAG,"number="+number);
   //    //判斷電話號碼的有效性
       if(PhoneNumberUtils.isGlobalPhoneNumber(number)){
        Intent intent = new Intent(Intent.ACTION_DIAL,Uri.parse("tel://"+ number));
        startActivity(intent);
       }
      }
     }
          });
      }
      private void openToast(String str){
       Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
      }
  }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved