Tôi có một danh sáchXem các liên hệ mà tôi nhận được từ mẫu Android ContactManager. Danh sách này hiển thị tốt, nhưng tôi không thể tìm ra cách nhận thông tin từ mục đã chọn, như "tên" và "số điện thoại".getItemAtPosition() Cách lấy dữ liệu có thể đọc từ mục đã chọn trong ListView
Tôi có thể nhận được vị trí đã chọn, nhưng kết quả của mContactList.getItemAtPosition (vị trí) là một ContentResolver $ CursorWrapperInner và điều đó không thực sự có ý nghĩa với tôi. Tôi không thể nhận được đầu hoặc đuôi từ đó.
Bất kỳ ai cũng biết cách tôi có thể lấy tên/id/số điện thoại từ mục đã chọn trong listView?
Đây là mã của tôi.
@Override
public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_contact);
// Obtain handles to UI objects
mAddAccountButton = (Button) findViewById(R.id.addContactButton);
mContactList = (ListView) findViewById(R.id.contactList);
mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
// Initialize class properties
mShowInvisible = false;
mShowInvisibleControl.setChecked(mShowInvisible);
mContactList.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
addContactAt(position);
}
});
mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
mShowInvisible = isChecked;
populateContactList();
}
});
// Populate the contact list
populateContactList();
}
/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private SimpleCursorAdapter adapter;
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}
/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
private void addContactAt(int position)
{
Object o = mContactList.getItemAtPosition(position);
}
} `
này sẽ trở lại giống hệt nhau 'Cursor' đối tượng – Vasiliy