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

发布于 2024-09-26 18:10:46 字数 2801 浏览 7 评论 0原文

我有一个从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遗心遗梦遗幸福 2024-10-03 18:10:46
@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) +"   ");
@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) +"   ");
弃爱 2024-10-03 18:10:46

繁荣!我想通了。基本上,您可以从单击事件中获取位置编号,然后在我的 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));
}
}

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:

@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));
}
}
凉风有信 2024-10-03 18:10:46
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");
}
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");
}
白龙吟 2024-10-03 18:10:46

嗯 - 你把 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.

顾铮苏瑾 2024-10-03 18:10:46

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

I used the following code as mentioned by 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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文