在android中显示联系人

时间:2015-05-30 21:07:11

标签: android listview android-listview

我正在使用此代码在lisview中显示联系人。除了一件事,它工作得很好。它显示所有联系人,包括电子邮件ID和其他联系人。

我想向谁显示那些有电话号码的联系人。我怎么能这样做?

这是我的代码: -

    public class PhoneBookActivity extends Activity {

        //Android listview object
        ListView listViewPhoneBook;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.phone_book);

          //get the ListView Reference from xml file
          listViewPhoneBook=(ListView)findViewById(R.id.listPhoneBook);

          //arrayColumns is the array which will contain all contacts name in your cursor, where the cursor will get the data from contacts database.
          //Here we are displaying name only from the contacts database
          String[] arrayColumns = new String[]{ContactsContract.Contacts.DISPLAY_NAME};
          //arrayViewID is the id of the view it will map to here textViewName only , you can add more Views as per Requirement
          int[] arrayViewID = new int[]{R.id.textViewName};

         //reference to the phone contacts database using  Cursor and URI in android. 
          Cursor cursor;
         cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        /*Create an Adapter with arguments layoutID, Cursor, Array Of Columns, and Array of Views which is to be Populated
        This adapter will be associated with the listview to populate items directly. So this adapter is associated with 
        the each_contact.xlm file to view in a activity */
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.each_contact, cursor, arrayColumns, arrayViewID);
         listViewPhoneBook.setAdapter(adapter);

         /*this is extra code for click event of any item in the list view.
         when you will click on na contact's name in the list view. it will give you the item name and position in a listview.
         Note that: if you want to query the contacts name details(like phone number for the clicked contact name  & all details,
         we need to query it again. i will explain it in my a separate post in my blog.*/

         // To handle the click on List View Item
         listViewPhoneBook.setOnItemClickListener(new OnItemClickListener() {
             public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
             {
                 // position parameter gives the index or position of ListView Item which is Clicked
                 TextView tv=(TextView)v.findViewById(R.id.textViewName);
                 String name=tv.getText().toString();
                 Toast.makeText(getApplicationContext(), "Contact Selected: "+name, Toast.LENGTH_LONG).show();
             }
        });

        }
    }

更新:---

我自己解决了我的一个问题。 现在我有另一个问题。 如何在电话联系中获取电话号码点击此列表视图?

我正在尝试这样做:

   // To handle the click on List View Item
            listViewPhoneBook.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> arg0, View v,int position, long arg3)
                {
                    // position parameter gives the index or position of ListView Item which is Clicked
                    TextView tv=(TextView)v.findViewById(R.id.textViewName);
                    String name=tv.getText().toString();
                    Toast.makeText(getApplicationContext(), "Contact Selected: " + position, Toast.LENGTH_LONG).show();

                    Cursor c = (Cursor) arg0.getItemAtPosition(position);
                   // Cursor c = (Cursor)arg0.getAdapter().getItem(position);
                    String number = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Toast.makeText(getApplicationContext(), "Contact Selected: " + number, Toast.LENGTH_LONG).show();


                }
            });

错误: -  java.lang.IllegalStateException:无法从CursorWindow读取第0行col -1。在从中访问数据之前,请确保正确初始化Cursor。             在android.database.CursorWindow.nativeGetString(本机方法)

3 个答案:

答案 0 :(得分:0)

您可以通过以下方式阅读与联系人相关的所有电话号码:

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

答案 1 :(得分:0)

知道了,我们需要在游标中添加选择。

 String selection = ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1";
 cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, selection, null, null);

答案 2 :(得分:0)

In Kotlin:
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    var cr = contentResolver
    var c=cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null)
    var from = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Phone.NUMBER)
    var to =   intArrayOf(R.id.name,R.id.number)
    var adapter=SimpleCursorAdapter(this,R.layout.indiview,c,from,to,0)
    lview.adapter = adapter

     lview?.onItemClickListener = object : AdapterView.OnItemClickListener {
        override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
            var phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            var list= mutableListOf<String>()
            while (phones.moveToNext()) {
                var name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                var phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                list.add(phoneNumber)
            }
           var i = Intent(Intent.ACTION_CALL)
            //Toast.makeText(this@MainActivity,list.get(p2),Toast.LENGTH_LONG).show()
            i.data = Uri.parse("tel:${list.get(p2)}")
            startActivity(i)

        }
相关问题