我正在尝试显示来自Android电话簿的联系人的姓名和号码,我有一个联系人存储在我的AVD中。谁能告诉我这个代码有什么问题请...显示应用程序停止工作请再试一次...提前感谢...
public class get extends Activity {
private void getColumnData(Cursor cur)
{
if(cur.moveToFirst())
{
int numi=cur.getColumnIndex(People.NUMBER);
int namei=cur.getColumnIndex(People.NAME);
String name,num;
do{
name=cur.getString(namei);
num=cur.getString(numi);
}while(cur.moveToNext());
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Uri u=People.CONTENT_URI;
String[] proj=new String[]{People.NAME,People.NUMBER};
Cursor cur=managedQuery(u, proj, null, null, null);
getColumnData(cur);
TextView tv=new TextView(this);
tv.setText("First step");
setContentView(tv);
}
}
答案 0 :(得分:0)
尝试使用android.provider.ContactsContract.Contacts;
还在清单文件中放入READ_CONTACTS权限
答案 1 :(得分:0)
import android.provider.ContactsContract; import android.provider.ContactsContract.Contacts;
ListView lv =(ListView)findViewById(R.id.list_view);
Uri uri=Contacts.CONTENT_URI;
String [] projection=new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
Cursor c=managedQuery(uri,null, null, null, null);
String[] from=new String[]{ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
int[] to=new int[]{android.R.id.text1,android.R.id.text2};
SimpleCursorAdapter sAdapter=new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2, c, from, to);
lv.setAdapter(sAdapter);
也放 在清单文件中