Java 获取内容提供程序中所有名称的列表
我正在使用“Calls.CONTENT\u URI”内容提供者。我已经检索了列名,但现在我想从这个内容提供者的列名“name”中获取所有名称 我的代码如下:Java 获取内容提供程序中所有名称的列表,java,android,android-contentprovider,Java,Android,Android Contentprovider,我正在使用“Calls.CONTENT\u URI”内容提供者。我已经检索了列名,但现在我想从这个内容提供者的列名“name”中获取所有名称 我的代码如下: uri = Calls.CONTENT_URI; String[] projection = {"name"}; String selection = null; String[] selectionArgs = null; String sort = null; resolver = getC
uri = Calls.CONTENT_URI;
String[] projection = {"name"};
String selection = null;
String[] selectionArgs = null;
String sort = null;
resolver = getContentResolver();
cursor = resolver.query(uri, projection, selection, selectionArgs, sort);
Log.i("TUTORIAL", "counts :"+cursor.getCount());
String s;
cursor.moveToFirst();
for(int x=0; x<cursor.getCount(); x++){
s = cursor.getString(x);
Log.i("TUTORIAL", ""+s);
//cursor.moveToNext();
}
uri=Calls.CONTENT\u uri;
字符串[]投影={“名称”};
字符串选择=null;
字符串[]selectionArgs=null;
字符串排序=null;
解析程序=getContentResolver();
cursor=resolver.query(uri、投影、选择、选择、排序);
Log.i(“教程”,“计数:”+cursor.getCount());
字符串s;
cursor.moveToFirst();
对于(intx=0;x我想这会对你有帮助
ArrayList<String> nameList = new ArrayList<String>();
String[] projection = {"name"};
String selection = null;
String[] selectionArgs = null;
String sort = null;
resolver = getContentResolver();
cursor = resolver.query(uri, projection, selection, selectionArgs, sort);
Log.i("TUTORIAL", "counts :"+cursor.getCount());
String s;
if(cursor.moveToFirst()) {
do {
nameList.add(cursor.getString(0));
//your code
//s = cursor.getString(x);
Log.i("TUTORIAL", ""+cursor.getString(0));
}while(cursor.moveToNext());
}
ArrayList nameList=new ArrayList();
字符串[]投影={“名称”};
字符串选择=null;
字符串[]selectionArgs=null;
字符串排序=null;
解析程序=getContentResolver();
cursor=resolver.query(uri、投影、选择、选择、排序);
Log.i(“教程”,“计数:”+cursor.getCount());
字符串s;
if(cursor.moveToFirst()){
做{
nameList.add(cursor.getString(0));
//你的代码
//s=cursor.getString(x);
Log.i(“TUTORIAL”,“cursor.getString(0));
}while(cursor.moveToNext());
}
你可以试试这个,它对我有用:
public void readContacts() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// Get contact id (id)
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
// Get contact name (displayName)
String displayName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}
}
}
cur.close();
}
这是代码
public class MainActivity extends AppCompatActivity {
SimpleCursorAdapter mAdapter;
MatrixCursor mMatrixCursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// The contacts from the contacts content provider is stored in this cursor
mMatrixCursor = new MatrixCursor(new String[] { "_id","name","photo","details"} );
// Adapter to set data in the listview
mAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.lv_layout,
null,
new String[] { "name","photo","details"},
new int[] { R.id.tv_name,R.id.iv_photo,R.id.tv_details}, 0);
// Getting reference to listview
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
// Setting the adapter to listview
lstContacts.setAdapter(mAdapter);
// Creating an AsyncTask object to retrieve and load listview with contacts
ListViewContactsLoader listViewContactsLoader = new ListViewContactsLoader();
// Starting the AsyncTask process to retrieve and load listview with contacts
listViewContactsLoader.execute();
}
/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Cursor> {
@Override
protected Cursor doInBackground(Void... params) {
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);
String displayName="";
String nickName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
//String photoPath="" + R.drawable.blank;
byte[] photoByte=null;
String homeEmail="";
String workEmail="";
String companyName="";
String title="";
if(dataCursor.moveToFirst()){
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
do{
// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
// Getting EMails
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
homeEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}
// Getting Organization details
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
}
// Getting Photo
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));
if(photoByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);
// Getting Caching directory
File cacheDirectory = getBaseContext().getCacheDir();
// Temporary file to store the contact image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");
// The FileOutputStream to the temporary file
try {
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
// Writing the bitmap to the temporary file as png file
bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);
// Flush the FileOutputStream
fOutStream.flush();
//Close the FileOutputStream
fOutStream.close();
} catch (Exception e) {
e.printStackTrace();
}
// photoPath = tmpFile.getPath();
}
}
}while(dataCursor.moveToNext());
String details = "";
// Concatenating various information to single string
if(homePhone != null && !homePhone.equals("") )
details = "HomePhone : " + homePhone + "\n";
if(mobilePhone != null && !mobilePhone.equals("") )
details += "MobilePhone : " + mobilePhone + "\n";
if(workPhone != null && !workPhone.equals("") )
details += "WorkPhone : " + workPhone + "\n";
if(nickName != null && !nickName.equals("") )
details += "NickName : " + nickName + "\n";
if(homeEmail != null && !homeEmail.equals("") )
details += "HomeEmail : " + homeEmail + "\n";
if(workEmail != null && !workEmail.equals("") )
details += "WorkEmail : " + workEmail + "\n";
if(companyName != null && !companyName.equals("") )
details += "CompanyName : " + companyName + "\n";
if(title != null && !title.equals("") )
details += "Title : " + title + "\n";
// Adding id, display name, path to photo and other details to cursor
mMatrixCursor.addRow(new Object[]{ Long.toString(contactId),displayName,null,details});
}
}while(contactsCursor.moveToNext());
}
return mMatrixCursor;
}
@Override
protected void onPostExecute(Cursor result) {
// Setting the cursor containing contacts to listview
mAdapter.swapCursor(result);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public类MainActivity扩展了AppCompatActivity{
简单的适应;
MatrixCursor mMatrixCursor;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//联系人内容提供程序中的联系人存储在此光标中
mMatrixCursor=新矩阵xcursor(新字符串[]{“\u id”,“name”,“photo”,“details”});
//用于在listview中设置数据的适配器
mAdapter=new SimpleCursorAdapter(getBaseContext(),
R.布局。lv_布局,
无效的
新字符串[]{“名称”、“照片”、“详细信息”},
新int[]{R.id.tv_name,R.id.iv_photo,R.id.tv_details},0);
//获取对listview的引用
ListView lstContacts=(ListView)findViewById(R.id.lst_contacts);
//将适配器设置为listview
lstContacts.setAdapter(mAdapter);
//创建AsyncTask对象以检索和加载带有联系人的listview
ListViewContactsLoader ListViewContactsLoader=新建ListViewContactsLoader();
//启动AsyncTask进程以检索和加载带有联系人的listview
listViewContactsLoader.execute();
}
/**用于检索和加载带有联系人的listview的AsyncTask类*/
私有类ListViewContactsLoader扩展异步任务{
@凌驾
受保护的游标doInBackground(无效…参数){
Uri contactsUri=ContactsContract.Contacts.CONTENT\u Uri;
//查询ContactsContract.Contacts表以检索所有联系人
Cursor contactsCursor=getContentResolver().query(contactsUri,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+“ASC”);
如果(contactsUrsor.moveToFirst()){
做{
long contactId=contactsCursor.getLong(contactsCursor.getColumnIndex(“\u ID”);
Uri dataUri=ContactsContract.Data.CONTENT\u Uri;
//查询ContactsContract.Data表以检索单个项目,如
//每个联系人对应的家庭电话、手机、工作电子邮件等
Cursor dataCursor=getContentResolver().query(dataUri,null,
contacts contract.Data.CONTACT_ID+“=”+contactId,
空,空);
字符串displayName=“”;
字符串昵称=”;
字符串homePhone=“”;
字符串mobilePhone=“”;
字符串workPhone=“”;
//字符串photoPath=”“+R.drawable.blank;
字节[]photoByte=null;
字符串homemail=“”;
字符串workEmail=“”;
字符串companyName=“”;
字符串标题=”;
if(dataCursor.moveToFirst()){
//获取显示名称
displayName=dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
做{
//获得昵称
if(dataCursor.getString(dataCursor.getColumnIndex(“mimetype”)).equals(ContactsContract.CommonDataTypes.昵称.CONTENT\u ITEM\u TYPE))
昵称=dataCursor.getString(dataCursor.getColumnIndex(“data1”));
//获取电话号码
if(dataCursor.getString(dataCursor.getColumnIndex(“mimetype”)).equals(ContactsContract.CommonDataTypes.Phone.CONTENT\u ITEM\u TYPE)){
开关(dataCursor.getInt(dataCursor.getColumnIndex(“data2”)){
案例联系人contract.commondatatypes.Phone.TYPE\u HOME:
homePhone=dataCursor.getString(dataCursor.getColumnIndex(“data1”));
打破
案例联系人contract.commondatatypes.Phone.TYPE\u手机:
mobilePhone=dataCursor.getString(dataCursor.getColumnIndex(“data1”));
打破
案例联系人contract.commondatatypes.Phone.TYPE\u工作:
workPhone=dataCursor.getString(dataCursor.getColumnIndex(“data1”));
打破
}
}
//收到电子邮件
if(dataCursor.getString(dataCursor.getColumnIndex(“mimetype”)).equals(ContactsContract.CommonDataTypes.Email.CONTENT\u ITEM\u TYPE)){
开关(dataCursor.getInt(dataCursor.getColumnIndex(“data2”)){
个案联络中心