getItemAtPosition()如何获得在ListView从选定项目可读数据 [英] getItemAtPosition() How to get readable data from the selected item in a ListView

查看:144
本文介绍了getItemAtPosition()如何获得在ListView从选定项目可读数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我从Android的ContactManager样品有接触的一个ListView。该列表显示了罚款,但我无法弄清楚如何从所选的项目获取信息,如名和电话号码。

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".

我可以选择的位置,但mContactList.getItemAtPosition(位置)的结果是一个ContentResolver的$ CursorWrapperInner并没有真正使任何意义,我。我不能得到正面或反面从。

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.

任何人都知道我可以从列表视图中选择的项目获得的名称/ ID /电话号码?

Anyone know how I can get the name/id/phone number from the selected item in the listView?

下面是我的code。

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

}`

推荐答案

BOOM!我想通了。基本上你得到的单击事件的位置号码,然后在我的addContatAt(),您使用的位置,光标你想要的领域内进行搜索。在我来说,我想要的显示名称。

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.

我已经习惯了做事情的Flex,因此这光标企业是不同的对我来说:)

I'm used to doing things in Flex, so this Cursor business is different for me :)

不管怎么说,别人这里是我的code:

Anyways, for others 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
    contactsCursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME
    };
    adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, contactsCursor,
            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)
{
    contactsCursor.moveToPosition(position);
    String name = contactsCursor.getString(
            contactsCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
}

}

这篇关于getItemAtPosition()如何获得在ListView从选定项目可读数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆