【问题标题】:Android: Get ID of selected item from List Dialog created with a CursorAndroid:从使用光标创建的列表对话框中获取所选项目的 ID
【发布时间】:2012-03-18 20:39:41
【问题描述】:

在我的 Activity 类中,我覆盖了 onCreateDialog() 方法 代码有点如下所示。 该对话框现在列出了我的联系人列表中的所有项目。

当用户单击此列表中的项目时,我想获取联系人 ID 点击项的值,即字段Phone._ID的值。

目前我只得到里面所选项目的位置(索引) which OnClickListener 的参数。 如果列表显示在 ListActivity 中,我可能会使用:

getListView().getItemIdAtPosition(which);

但在这里我无法获得对 ListView 的引用。 如何从使用光标创建的列表对话框中获取单击项目的 ID。

protected Dialog onCreateDialog(int id) {
  String[] projection = new String[] {
                Phone._ID,
                Phone.DISPLAY_NAME,
                Phone.NUMBER
        };
  Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, null, null, null);
  return new AlertDialog.Builder(ContactActivity.this)
            .setTitle("Select Contacts")
            .setCursor(cursor,
               new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {

               /* User clicked on a contact item */

               Toast.makeText(getApplicationContext(), 
                            "CLICKED-"+which,
            Toast.LENGTH_SHORT).show();

            }
               }, ContactsContract.Contacts.DISPLAY_NAME)
           .create();

}

提前致谢

【问题讨论】:

    标签: android listview dialog contacts


    【解决方案1】:

    我自己找到了解决方案。下面是我现在使用的代码。

    我已将我的联系人列表查询的光标声明为 final。 并在 onClick 方法中使用相同的光标对象 获取点击位置的记录,使用moveToPosition()方法 光标。 另请注意,我必须在底部使用cursor.moveToFirst(); 的点击。否则,当我调用 再次对话(我不确定为什么会这样)。

    protected Dialog onCreateDialog(int id) {
    String[] projection = new String[] {
                Phone._ID,
                Phone.DISPLAY_NAME,
                Phone.NUMBER
        };
    final Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                projection, null, null, null);
    return new AlertDialog.Builder(ContactActivity.this)
            .setTitle("Select Contacts")
            .setCursor(cursor,
            new DialogInterface.OnClickListener() {
    
              public void onClick(DialogInterface dialog, int which) {
    
               /* User clicked on a contact item */
                if(cursor.moveToPosition(which)){
    
                 long contactId=cursor.getLong(0);
                 String name=cursor.getString(1);
                 String number=cursor.getString(2);
                 Toast.makeText(getApplicationContext(),
                         "You selected: ["+contactId+"]" 
                          + name + " , " +number,
                         Toast.LENGTH_SHORT)
                       .show();
                }
                cursor.moveToFirst();//Reset the position
    
              }
           }, ContactsContract.Contacts.DISPLAY_NAME)
           .create();
    
    }
    

    但我觉得这不是正确的做法。 所以我没有将此标记为这个问题的答案。 我会把这个问题留一段时间,并希望 有人可以提出更好的建议。

    更新:标记为答案,因为没有回应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2017-06-09
      • 2011-06-08
      相关资源
      最近更新 更多