Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中获取并选择联系人_Android_Android Studio_Contacts - Fatal编程技术网

在android中获取并选择联系人

在android中获取并选择联系人,android,android-studio,contacts,Android,Android Studio,Contacts,我正在尝试获取所有联系人并将其选中 我已完成在手机中获取所有联系人。但当我尝试选择几个联系人并获取他们的姓名或号码时,我遇到了空指针错误 public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> { private CursorAdapter mAdapter; final HashMap<String,String> hashMap = new Ha

我正在尝试获取所有联系人并将其选中

我已完成在手机中获取所有联系人。但当我尝试选择几个联系人并获取他们的姓名或号码时,我遇到了空指针错误

public class ContactListFragment extends ListFragment implements LoaderCallbacks<Cursor> {

private CursorAdapter mAdapter;
final HashMap<String,String> hashMap = new HashMap<String,String>();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
private Uri uriContact;

private String contactID;
String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Phone.NUMBER};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // create adapter once
    Context context = getActivity();
    int layout = android.R.layout.simple_list_item_multiple_choice;
    Cursor c = null; // there is no cursor yet
    int flags = 0; // no auto-requery! Loader requeries.
    mAdapter = new SimpleCursorAdapter(context, layout, c, FROM, TO, flags);
}



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // each time we are started use our listadapter
    setListAdapter(mAdapter);
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

    // and tell loader manager to start loading
    getLoaderManager().initLoader(0, null, this);

    ***getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cursorID = getActivity().getContentResolver().query(uriContact,
                    new String[]{ContactsContract.Contacts._ID},
                    null, null, null);
            contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            System.out.println(contactID);
        }
    });
}***

// columns requested from the database
private static final String[] PROJECTION = {
        Contacts._ID, // _ID is always required
        Contacts.DISPLAY_NAME_PRIMARY // that's what we want to display
};

// and name should be displayed in the text1 textview in item layout
private static final String[] FROM = { Contacts.DISPLAY_NAME_PRIMARY };
private static final int[] TO = { android.R.id.text1 };

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {

    // load from the "Contacts table"
    Uri contentUri = Contacts.CONTENT_URI;

    // no sub-selection, no sort order, simply every row
    // projection says we want just the _id and the name column
    return new CursorLoader(getActivity(),
            contentUri,
            PROJECTION,
            null,
            null,
            null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Once cursor is loaded, give it to adapter
    mAdapter.swapCursor(data);

}

@Override
public void onLoaderReset(Loader<Cursor> loader) {
    // on reset take any old cursor away
    mAdapter.swapCursor(null);
}
公共类ContactListFragment扩展ListFragment实现LoaderCallbacks{
私人游标适配器;
final HashMap HashMap=新HashMap();
Uri Uri=ContactsContract.CommonDataTypes.Phone.CONTENT\u Uri;
私人联系;
私有字符串联系人ID;
字符串[]投影=新字符串[]{ContactsContract.CommonDataTypes.Phone.DISPLAY\u NAME,
ContactsContract.CommonDataTypes.Phone.NUMBER};
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//创建适配器一次
Context=getActivity();
int layout=android.R.layout.simple\u list\u item\u多选;
游标c=null;//还没有游标
int flags=0;//没有自动重新查询!加载程序重新查询。
mAdapter=newsimplecursoradapter(上下文、布局、c、FROM、TO、标志);
}
@凌驾
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//每次启动时,请使用listadapter
setListAdapter(mAdapter);
getListView().setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE);
//并告诉装载机管理员开始装载
getLoaderManager().initLoader(0,null,this);
***getListView().setOnItemClickListener(新的AdapterView.OnItemClickListener()){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
游标游标游标ID=getActivity().getContentResolver().query(uriContact,
新字符串[]{Contacts contract.Contacts.\u ID},
空,空,空);
contactID=cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
System.out.println(联系人ID);
}
});
}***
//从数据库请求的列
私有静态最终字符串[]投影={
联系人。\u ID,//\u ID始终是必需的
Contacts.DISPLAY\u NAME\u PRIMARY//这就是我们想要显示的内容
};
//和名称应显示在项目布局的text1 textview中
私有静态最终字符串[]FROM={Contacts.DISPLAY_NAME_PRIMARY};
私有静态final int[]TO={android.R.id.text1};
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
//从“联系人表”加载
Uri contentUri=Contacts.CONTENT\u Uri;
//没有子选择,没有排序顺序,只是每一行
//投影表示我们只需要_id和name列
返回新的游标装入器(getActivity(),
孔图里,
投影,
无效的
无效的
无效);
}
@凌驾
public void onLoadFinished(加载器、光标数据){
//一旦光标被加载,就将其交给适配器
mAdapter.swapCursor(数据);
}
@凌驾
公共void onLoaderReset(加载器){
//重置时,将任何旧光标移走
mAdapter.swapCursor(空);
}
我认为问题在于我的听众

我怎样才能解决这个问题?
从现在开始感谢:)

主要问题是当您进行查询时,
uriContact
为空

另一个问题是,您只使用
Contacts contract.Contacts.\u ID
作为投影,因此ID是唯一返回的内容

我通过对投影使用
null
使其工作,以便它返回所有行

我还添加了查找当前选定联系人的功能,并显示带有他们电话号码的祝酒词

这不是最佳代码,因为它只是查询所有行,然后遍历它们,直到找到当前选定的联系人,但它可以工作:

  getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cur = mAdapter.getCursor();
            cur.moveToPosition(position);
            String curName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
            System.out.println(curName);
            uriContact = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            Cursor cursorID = getContentResolver().query(uriContact,
                    null,
                    null, null, null);
            for (cursorID.moveToFirst(); !cursorID.isAfterLast(); cursorID.moveToNext() ) {
                String testName = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
                if (testName != null && testName.equals(curName)) {
                    contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
            }
            if (contactID != null) {
                System.out.println(contactID);
                Toast.makeText(MainActivity.this, "contact Phone: " + contactID, Toast.LENGTH_LONG).show();
            }
        }
    });
getListView(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Cursor cur=mAdapter.getCursor();
当前移动位置(位置);
String curName=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
System.out.println(curName);
uriContact=ContactsContact.CommonDataTypes.Phone.CONTENT\u URI;
游标游标游标ID=getContentResolver().query(uriContact,
无效的
空,空,空);
for(cursorID.moveToFirst();!cursorID.isAfterLast();cursorID.moveToNext()){
String testName=cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
if(testName!=null&&testName.equals(curName)){
contactID=cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataTypes.Phone.NUMBER));
}
}
if(contactID!=null){
System.out.println(联系人ID);
Toast.makeText(MainActivity.this,“联系人电话:+contactID,Toast.LENGTH_LONG).show();
}
}
});
编辑:我得到了一个更优化的版本,它使用查询中的selection和SelectionAgs只返回当前联系人信息:

  getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor cur = mAdapter.getCursor();
            cur.moveToPosition(position);
            String curName = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
            System.out.println(curName);
            uriContact = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
            Cursor cursorID = getContentResolver().query(uriContact,
                    null,
                    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY + " = ?", new String[]{curName}, null);
            if (cursorID.moveToFirst()) {
                contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            }
            if (contactID != null) {
                System.out.println(contactID);
                Toast.makeText(MainActivity.this, "contact Phone: " + contactID, Toast.LENGTH_LONG).show();
            }
        }
    });
getListView(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
Cursor cur=mAdapter.getCursor();
当前移动位置(位置);
String curName=cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
System.out.println(curName);
uriContact=ContactsContact.CommonDataTypes.Phone.CONTENT\u URI;
游标游标游标ID=getContentResolver().query(uriContact,
无效的
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY+“=?”,新字符串[]{curName},null);
如果(当前)