学习笔记,android:获取联系人列表,点击联系人打电话 用android.R.layout.simple_list_item_2形式

郭霖的第一行代码举了读取联系人的例子,前有打电话的例子,该结合在一起,点击联系人就好打电话。要响应点击单个联系人的事件。布局不改变,但是列表形式要改变:
android.R.layout.simple_list_item_2 或 android.R.layout.simple_expandable_list_item_2。

activity_main.xml
  
  

     
     

     
     

    
      
      

     
     
AndroidManifest.xml:
  
  

     
     
    
      
      
    
      
      

    
      
      
        
       
       
            
        
        
                
         
         

                
         
         
            
        
        
        
       
       
    
      
      


     
     

MainActivity.java:

 
 
package com.example.contactstest;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private ListView listView;
    private Map
    
    
     
      contact;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission
            .READ_CONTACTS }, 1);
        }
        else {
            readContacts();
        }
    }
    private void readContacts(){
        listView = (ListView)findViewById(R.id.contacts_view);
        SimpleAdapter simpleAdapter;
        List
     
     
      
      
       
       > listmaps=new ArrayList
       
       
        
        
          >(); Cursor cursor = null; try { cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone. CONTENT_URI,null, null, null ,null); if(cursor != null){ while (cursor.moveToNext()){ String dispalyName = cursor.getString(cursor.getColumnIndex(ContactsContract. CommonDataKinds.Phone.DISPLAY_NAME)); String number = cursor.getString(cursor.getColumnIndex(ContactsContract. CommonDataKinds.Phone.NUMBER)); Map 
         
           map=new HashMap 
          
            (); map.put("name", dispalyName); map.put("number", number); listmaps.add(map); } /*simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_list_item_2, new String[]{"name","number"}, new int[]{android.R.id.text1,android.R.id.text2});*/ simpleAdapter=new SimpleAdapter(MainActivity.this, listmaps, android.R.layout.simple_expandable_list_item_2, new String[]{"name","number"}, new int[]{android.R.id.text1,android.R.id.text2}); listView.setAdapter(simpleAdapter); simpleAdapter.notifyDataSetChanged(); myCall(listmaps); } } catch (Exception e){ e.printStackTrace(); }finally { if(cursor != null){ cursor.close(); } } } @Override public void onRequestPermissionsResult(int requesCode, String[] permissions, int[] grantResults) { switch (requesCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){ readContacts(); } else { Toast.makeText(this, "You denied the permission.", Toast.LENGTH_LONG).show(); } break; case 2: if(grantResults.length > 0 && grantResults[0] ==PackageManager. PERMISSION_GRANTED){ callphone(); } else { Toast.makeText(this, "You denied the permission.",Toast.LENGTH_LONG).show(); } break; default: } } public void myCall(final List 
            
            
              > mapList){ listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView 
              parent, View view, int position, long id){ if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission. CALL_PHONE) !=PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest. permission.CALL_PHONE},2); } else { Map 
             
               mMap=mapList.get(position); contact = mapList.get(position); String number = contact.get("number"); callphone(); } } }); } private void callphone(){ try{ Intent intent = new Intent(Intent.ACTION_CALL); String num = (String)contact.get("number"); intent.setData(Uri.parse("tel:"+num)); startActivity(intent); }catch (SecurityException e){ e.printStackTrace(); } } } 
              
             
           
          
        
      
      
    
    
       
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值