Android开发获取联系人到ListView中

 参考代码:

  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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值