gpt4 book ai didi

android - getItemAtPosition() 如何从 ListView 中的选定项获取可读数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:53 28 4
gpt4 key购买 nike

我有一个从 Android ContactManager 示例中获得的联系人 ListView 。此列表显示正常,但我不知道如何从所选项目中获取信息,例如“姓名”和“电话号码”。

我可以获得选定的位置,但 mContactList.getItemAtPosition(position) 的结果是 ContentResolver$CursorWrapperInner,这对我来说没有任何意义。我无法从中得到正面或反面。

有谁知道如何从 ListView 中的选定项目中获取姓名/身份证/电话号码?

这是我的代码。

@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);
}

}`

最佳答案

@Override
protected void onListItemClick(ListView l, View v, int position, long ida) {
super.onListItemClick(l, v, position, ida);

Cursor mycursor = (Cursor) getListView().getItemAtPosition(position);
showToast("mycursor.getString(1) " + mycursor.getString(1) +" ");

关于android - getItemAtPosition() 如何从 ListView 中的选定项获取可读数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886617/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com