简体   繁体   English

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

[英]getItemAtPosition() How to get readable data from the selected item in a ListView

I have a listView of contacts that I got from the Android ContactManager sample. 我有一个从Android ContactManager示例中获得的联系人列表视图。 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. 我可以获得所选位置,但mContactList.getItemAtPosition(position)的结果是一个ContentResolver $ CursorWrapperInner,这对我来说并没有任何意义。 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? 任何人都知道如何从listView中的选定项目获取名称/ ID /电话号码?

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

}` }`

@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) +"   ");

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. 基本上你从click事件中获取位置编号,然后在我的addContatAt()中使用该位置在光标内搜索你想要的字段。 In my case I wanted the display name. 在我的情况下,我想要显示名称。

I'm used to doing things in Flex, so this Cursor business is different for me :) 我习惯在Flex中做事,所以这个Cursor业务对我来说是不同的:)

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));
}
}
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
Map<String, Object> map = (Map<String, Object>)_productListView.getItemAtPosition(position); 
String _productCode = (String) map.get("ProductCode");
String _productName = (String) map.get("ProjectName");
Double _price = (Double) map.get("Price");
}

Hmm - you're messing with your AdapterView's cursor behind its back, which may not always be a good idea. 嗯 - 你正在弄乱你背后的AdapterView光标,这可能并不总是一个好主意。 The alternative is to call parent.getItemAtPosition(position) inside your onItemClick handler and cast the result to a Cursor; 另一种方法是在onItemClick处理程序中调用parent.getItemAtPosition(position)并将结果转换为Cursor; it will be pointing at the row corresponding to the item that was clicked on. 它将指向与单击的项对应的行。

I used the following code as mentioned by Miki Habryn 我使用了Miki Habryn提到的以下代码

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Cursor client = (Cursor)parent.getItemAtPosition(position);
    String client_name = client.getString(2); // third column in db
    Toast.makeText(getBaseContext(), client_name, Toast.LENGTH_SHORT).show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM