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

时间:2010-10-07 23:18:24

标签: android listview selecteditem

我有一个从Android ContactManager示例中获得的联系人列表视图。此列表显示正常,但我无法弄清楚如何从所选项目中获取信息,例如“姓名”和“电话号码”。

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

任何人都知道如何从listView中的所选项目中获取姓名/ 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);
}

}`

5 个答案:

答案 0 :(得分:15)

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

答案 1 :(得分:6)

砰!我想通了。基本上你从click事件中获取位置编号,然后在我的addContatAt()中使用该位置在光标内搜索你想要的字段。在我的情况下,我想要显示名称。

我习惯在Flex中做事,所以这个Cursor业务对我来说是不同的:)

无论如何,对于其他人来说,这是我的代码:

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

答案 2 :(得分:4)

嗯 - 你正在弄乱你背后的AdapterView光标,这可能并不总是一个好主意。另一种方法是在onItemClick处理程序中调用parent.getItemAtPosition(position)并将结果转换为Cursor;它将指向与单击的项目对应的行。

答案 3 :(得分:4)

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

答案 4 :(得分:0)

我使用了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();
}