getItemAtPosition() 如何从 ListView 中的选定项获取可读数据
我有一个从 Android ContactManager 示例中获取的联系人列表视图。该列表显示正常,但我不知道如何从所选项目中获取信息,例如“姓名”和“电话号码”。
我可以获得选定的位置,但 mContactList.getItemAtPosition(position) 的结果是 ContentResolver$CursorWrapperInner ,这对我来说没有任何意义。我无法从中得到正面或反面。
有人知道如何从列表视图中的所选项目中获取姓名/ID/电话号码吗?
这是我的代码。
@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);
}
}`
I have a listView of contacts that I got from the Android ContactManager sample. This list is showing up fine, but I can't figure out how to get info from the selected item, like "name" and "phone number".
I can get the selected position, but the result of the mContactList.getItemAtPosition(position) is a ContentResolver$CursorWrapperInner and that doesn't really make any sense to me. I can't get heads or tails from that.
Anyone know how I can get the name/id/phone number from the selected item in the listView?
Here is my code.
@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);
}
}`
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
繁荣!我想通了。基本上,您可以从单击事件中获取位置编号,然后在我的 addContatAt() 中使用该位置在光标内搜索所需的字段。就我而言,我想要显示名称。
我习惯在 Flex 中做事,所以这个 Cursor 业务对我来说是不同的:)
不管怎样,对于其他人来说,这是我的代码:
BOOM!I figured it out. Basically you get the position number from the click event, then in my addContatAt() you use that position to search within the cursor for the field you want. In my case I wanted the display name.
I'm used to doing things in Flex, so this Cursor business is different for me :)
Anyways, for others here is my code:
嗯 - 你把 AdapterView 的光标弄乱了,这可能并不总是一个好主意。另一种方法是在 onItemClick 处理程序中调用 Parent.getItemAtPosition(position) 并将结果转换为 Cursor;它将指向与所单击的项目相对应的行。
Hmm - you're messing with your AdapterView's cursor behind its back, which may not always be a good idea. The alternative is to call parent.getItemAtPosition(position) inside your onItemClick handler and cast the result to a Cursor; it will be pointing at the row corresponding to the item that was clicked on.
我使用了 Miki Habryn 提到的以下代码
I used the following code as mentioned by Miki Habryn