有一个微调器,其中装有来自设备的触点。
Cursor contactsCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, {ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME}, ContactsContract.Contacts.HAS_PHONE_NUMBER +" = "+ HAS_PHONE_NUMBER_STATE, null, ContactsContract.Contacts.DISPLAY_NAME +" ASC");
SimpleCursorAdapter sca = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, contactsCursor, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, new int[] {android.R.id.text1});
contactsSpinner.setAdapter(sca);
当我尝试这样设置选择时
String contactId = getContactSelectedId();
if(null != contactId) {
contactsSpinner.setSelection(Integer.valueOf(contactId));
}
提出异常:
llegalStateException: couldn't move cursor to position x
堆栈跟踪:
07-31 01:07:08.854: E/AndroidRuntime(22745): FATAL EXCEPTION: main
07-31 01:07:08.854: E/AndroidRuntime(22745): java.lang.IllegalStateException: couldn't move cursor to position 1136
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:248)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.widget.Spinner.makeAndAddView(Spinner.java:544)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.widget.Spinner.layout(Spinner.java:495)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.widget.Spinner.onLayout(Spinner.java:459)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.view.View.layout(View.java:14055)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.view.ViewGroup.layout(ViewGroup.java:4604)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
07-31 01:07:08.854: E/AndroidRuntime(22745): at android.view.View.layout(View.java:14055)
我如何解决这个问题?
答案 0 :(得分:4)
setSelection(int position)
函数将位置作为参数,
但是你给函数提供了所选项目的ID。
您需要创建一个函数getContactSelectedPosition()
或类似的东西。
此函数将(可能)循环适配器的数据,然后返回匹配联系人的位置