【问题标题】:getItemAtPosition() How to get readable data from the selected item in a ListViewgetItemAtPosition() 如何从 ListView 中的选定项目中获取可读数据
【发布时间】:2011-04-22 15:29:15
【问题描述】:

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

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

有人知道如何从 listView 中的选定项目中获取姓名/身份证/电话号码吗?

这是我的代码。

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

}`

【问题讨论】:

    标签: android listview selecteditem


    【解决方案1】:

    BOOM!我想通了。基本上,您从单击事件中获取位置编号,然后在我的 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】:

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

      【讨论】:

        【解决方案3】:
        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】:
          @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) +"   ");
          

          【讨论】:

          • 我知道它在文档中 - ListView - 但是当相关的 ListView 已经作为参数提供时,我不明白为什么我们应该使用 getListView()
          • 确实如此。如果我用l 替换getListView(),这对我来说似乎很好。
          【解决方案5】:

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

          【讨论】:

          • 这将返回完全相同的Cursor对象
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-03-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多