package com.softeem.phone;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ListViewCompat;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView list;
private ArrayList arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
arrayList=new ArrayList<>();
list.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,arrayList));
readContacts(); //这里是先配置的适配器,再往arraylist中添加内容的。为啥还能正确显示数据(也就是说页面显示了readContacts()执行后的数据)刚开始
List中不应该是空的?然后页面上显示空才对呀 }
private void readContacts() {
Cursor cur=null;
try {
cur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int i=0;
while(cur.moveToNext())
{
i++;
if(i>7)
break;
String name = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
arrayList.add(name);
}
} finally {
if(cur!=null)
cur.close();
}
}
}