支持国产操作系统国产应用!

捉蛋网

当前位置: 捉蛋网>教程>Android开发>

Android开发获取联系人到ListView中

时间:2013-06-05 16:34来源: 作者: 点击:
  参考代码:

  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();
      }
  }

(责任编辑:捉蛋网-刷机)
............................................................................................... ...............................................................................................

 

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------