weixin_42071177
TutouEvilee
2019-12-11 16:21

为什么Android studio的通讯录里面有数据但是显示不出来

  • android-studio
  • xml
  • android

我的程序是从主活动里面打开次活动并且将通讯录里的联系人用List View显示出来。

在主活动中成功打开了次活动但是却显示不出来列表。可是我的通讯录里面是有数据的,通讯录如图:

图片说明

程序运行图:

图片说明

下面是次活动的代码:
xml设计:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/contacts_text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="cn.edu.jluzh.sy06_contacts.ContactsText">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/contacts_view"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

MaintActivity代码块:

public class ContactsText extends AppCompatActivity {

    ArrayAdapter<String> adapter;
    List<String> contactsList = new ArrayList<>();//列表的字段

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_text);

        ListView contactsView = (ListView) findViewById(R.id.contacts_view);//列表
        adapter = new ArrayAdapter<String>(ContactsText.this, android.R.layout.simple_list_item_1,contactsList);
        contactsView.setAdapter(adapter);
        //查看用户是否已授权
        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() {
        Cursor cursor = null;
        StringBuilder stringBuilder = null;
        try {
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
            if (cursor != null) {
                while (cursor.moveToFirst()) {
                    //get name
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    //get number
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    String contact = (stringBuilder.append(displayName).append("/n").append(number)).toString();
                    contactsList.add(contact);
                }
                adapter.notifyDataSetChanged();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private void OnRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        //(请求码,申请的权限名,授权结果
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}

求大神指点啊,我哭了,秃头少女真的找不到原因a

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答