|
| 1 | +package com.example.contextprovider; |
| 2 | + |
| 3 | +import androidx.appcompat.app.AppCompatActivity; |
| 4 | + |
| 5 | +import android.content.Context; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.os.Bundle; |
| 8 | +import android.provider.ContactsContract; |
| 9 | +import android.text.TextUtils; |
| 10 | +import android.util.Log; |
| 11 | +import android.widget.ArrayAdapter; |
| 12 | +import android.widget.ListView; |
| 13 | +import android.widget.Toast; |
| 14 | + |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +public class MainActivity extends AppCompatActivity { |
| 19 | + |
| 20 | + @Override |
| 21 | + protected void onCreate(Bundle savedInstanceState) { |
| 22 | + List<String> contactsList = new ArrayList<String>(); |
| 23 | + super.onCreate(savedInstanceState); |
| 24 | + setContentView(R.layout.activity_main); |
| 25 | + |
| 26 | + List<String> list = new ArrayList<String>(); |
| 27 | + Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); |
| 28 | + String phoneNumber; |
| 29 | + String phoneName; |
| 30 | + //while(cursor.moveToNext()){}这两种遍历方式一样 |
| 31 | + for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { |
| 32 | + String displayName = cursor.getString(cursor.getColumnIndex( |
| 33 | + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); |
| 34 | + String number = cursor.getString(cursor.getColumnIndex( |
| 35 | + ContactsContract.CommonDataKinds.Phone.NUMBER)); |
| 36 | + |
| 37 | + System.out.println("phoneNumber:" + number + "phoneName:" + displayName); |
| 38 | + contactsList.add(displayName + "\n" + number); |
| 39 | + |
| 40 | + |
| 41 | + ListView listview = (ListView) findViewById(R.id.lst1); |
| 42 | + ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, contactsList); |
| 43 | + listview.setAdapter(adapter); |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | +} |
| 51 | + |
0 commit comments