无法从与SimpleCursorAdapter绑定的ListView中获取所选项目

时间:2018-12-19 15:43:17

标签: android-studio android-listview simplecursoradapter android-cursorloader simpleadapter

我正在使用LoaderManager获取电话联系并将其填充到Listview中 使用此代码oncreate

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    if (id == 1) {
        return new CursorLoader(this, ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    }

    return null;

}

,并使用onLoadFinished上的这段代码在列表视图中填充

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (cursor != null && cursor.getCount() > 0) {
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
        int[] to = {R.id.textview_android_contact_name, R.id.textview_android_contact_phoneNr, R.id.textview_android_contact_id};
        simpleCursorAdapter = new SimpleCursorAdapter(this, R.layout.contacts_list_item, cursor, from, to, 0);
        contactlist.setAdapter(simpleCursorAdapter);
      }
}

问题是在填充ListView之后,我无法获得选择项的数据 我正在使用此代码,但没有任何作用

private void populateListView() {
    contactlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cursor newcursor = (Cursor) simpleCursorAdapter.getItem(position);
            newcursor.moveToFirst();
            ShowToast(newcursor.getString(2));

        }
    });

这就是光标所得到的

V/Cursor Object: >>>>> Dumping cursor android.content.ContentResolver$CursorWrapperInner@9d273c68
0 {
   data_version=0
   phonetic_name=null
   data_set=null
   phonetic_name_style=0
   contact_id=1
   lookup=0r1-29274B4B2F3F
   phonebook_label_alt=B
   data12=null
   data11=null
   data10=null
   mimetype=vnd.android.cursor.item/phone_v2
   data15=null
   data14=null
   data13=null
   display_name_source=40
   photo_uri=null
   data_sync1=null
   data_sync3=null
   data_sync2=null
   contact_chat_capability=null
   data_sync4=null
   account_type=null
   account_type_and_data_set=null
   custom_ringtone=null
   photo_file_id=null
   has_phone_number=1
   status=null
   data1=1 23
   chat_capability=null
   data4=null
   data5=null
   data2=2
   data3=null
   data8=null
   phonebook_bucket=2
   data9=null
   data6=null
   group_sourceid=null
   times_used=1
   account_name=null
   data7=null
   display_name=contactname
   phonebook_bucket_alt=2
   phonebook_label=B
   raw_contact_is_user_profile=0
   in_visible_group=1
   display_name_alt=contactname
   contact_status_res_package=null
   is_primary=0
   contact_status_ts=null
   raw_contact_id=1
   times_contacted=1
   contact_status=null
   status_res_package=null
   status_icon=null
   contact_status_icon=null
   version=3
   mode=null
   last_time_contacted=1544307104604
   contact_last_updated_timestamp=1544307125117
   res_package=null
   _id=1
   pinned=2147483647
   name_verified=0
   dirty=1
   status_ts=null
   is_super_primary=0
   photo_thumb_uri=null
   photo_id=null
   send_to_voicemail=0
   name_raw_contact_id=1
   contact_status_label=null
   status_label=null
   sort_key_alt=contactname
   starred=0
   sort_key=contactname
   contact_presence=null
   sourceid=null
   last_time_used=1544307104604
}

但我要在ListView中显示所选项目的联系人姓名或电话 所以我在做什么错

0 个答案:

没有答案