0

組み込みの AutoCompleteTextView を使用しており、次のコードでドロップダウン リストを作成します。

autoComp = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    Uri filterUri;
    if (filterText == null) {filterUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;}else{filterUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(filterText + "%"));}
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};
    Cursor people = getContentResolver().query(filterUri, projection, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
    startManagingCursor(people);
    sca = new SimpleCursorAdapter(this, R.layout.autotextitem, people, 
            new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, 
            new int[] {R.id.textView1, R.id.textView2});
    autoComp.setAdapter(sca);
    autoComp.setThreshold(0);

ユーザーがドロップダウンからアイテムを選択すると、範囲外のカーソル例外が発生します。「CursorIndexOutOfBoundsException: サイズ 0 のインデックス 0 が要求されました」

アクティビティの onCreate メソッド内で OnClickEvent を宣言するコードを次に示します。

        autoComp.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> listView, View view,
                    int position, long id) {

            // Get the cursor, positioned to the corresponding row in the
            // result set
            Cursor cursor = (Cursor) sca.getItem(position);
            // Get the state's capital from this row in the database.
            String name =
                cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

            // Update the parent class's TextView
            TextView textViewTo = (TextView) findViewById(R.id.textViewNames);
            textViewTo.setText((CharSequence) cursor);

        }
    });

カーソルが空になる原因は何ですか?

4

1 に答える 1

1

cursor.moveToFirst(); を試してください。次に、cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

于 2012-05-16T07:43:05.823 に答える