从对话框访问 AdapterView

发布于 2024-10-07 08:57:30 字数 2333 浏览 3 评论 0原文

我有我的 ListActivity,当您点击某个项目时,它会弹出一个对话框,要求用户输入用户名和密码。如何从对话框中获取选定的位置?

以下是我初始化 ListActivity 的方法:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 ListView listView = getListView();
 listView.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
   showDialog(DIALOG_USER_PASSWORD);
  }
 });
}

我弹出的对话框是一个简单的 AlertDialog,带有 2 个 EditText,我从 xml 文件中扩充了它。

protected Dialog onCreateDialog(int id) {
        switch (id) {
        ...
        case DIALOG_USER_PASSWORD:
            LayoutInflater factory = LayoutInflater.from(this);
                        final View dialogView = factory.inflate(R.layout.alert_dialog_text_entry, null);
                        return new AlertDialog.Builder(MyListActivity.this)
                        .setIcon(R.drawable.alert_dialog_icon)
                        .setTitle(R.string.ask_user_password)
                        .setView(dialogView)
                        .setPositiveButton(R.string.ok_text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String userName = ((EditText) findViewById(R.id.username_edit_alert_dialog))
                            .getText().toString();
                    String password = ((EditText) findViewById(R.id.password_edit_alert_dialog))
                            .getText().toString();
                    Credentials cred = new CredentialsL1(userName, password);

                    /* HERE IS WHERE i NEED THE SELECTED ITEM 
                    mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
                    mService.connect(mId, cred);

                    }
                })
                // Cancel button
                .setNegativeButton(R.string.cancel_text,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    })
                .create();
        }
        return null;
    }

我唯一想到的是创建一个新字段“mId”并在用户点击时设置它当用户在对话框中点击“确定”时使用它。还有更优雅的想法吗? 谢谢

I have my ListActivity that when you tap on an item, it pops up a Dialog that ask the user for user and password. How can I get the selected position from the dialog?

Here's how I initialize the ListActivity:

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 ListView listView = getListView();
 listView.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
   showDialog(DIALOG_USER_PASSWORD);
  }
 });
}

The Dialog I pop up is a simple AlertDialog with 2 EditText which I inflate from an xml file

protected Dialog onCreateDialog(int id) {
        switch (id) {
        ...
        case DIALOG_USER_PASSWORD:
            LayoutInflater factory = LayoutInflater.from(this);
                        final View dialogView = factory.inflate(R.layout.alert_dialog_text_entry, null);
                        return new AlertDialog.Builder(MyListActivity.this)
                        .setIcon(R.drawable.alert_dialog_icon)
                        .setTitle(R.string.ask_user_password)
                        .setView(dialogView)
                        .setPositiveButton(R.string.ok_text, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String userName = ((EditText) findViewById(R.id.username_edit_alert_dialog))
                            .getText().toString();
                    String password = ((EditText) findViewById(R.id.password_edit_alert_dialog))
                            .getText().toString();
                    Credentials cred = new CredentialsL1(userName, password);

                    /* HERE IS WHERE i NEED THE SELECTED ITEM 
                    mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
                    mService.connect(mId, cred);

                    }
                })
                // Cancel button
                .setNegativeButton(R.string.cancel_text,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            dialog.cancel();
                        }
                    })
                .create();
        }
        return null;
    }

The only I've come up with is creating a new field "mId" and setting it when the user taps and using it when the user taps OK in the Dialog. Any more elegant idea?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

楠木可依 2024-10-14 08:57:30
private int selectedPosition;
...
protected void onCreate(Bundle savedInstanceState) {
....
// inside the item listener...
selectedPosition = position;
showDialog(DIALOG_USER_PASSWORD);

/* HERE IS WHERE i NEED THE SELECTED ITEM 
mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
// just use selectedPosition var

<块引用>

还有更优雅的想法吗?

看来您使用的是普通的 ListView (而不是复选框)...所以,这样做就可以了。

private int selectedPosition;
...
protected void onCreate(Bundle savedInstanceState) {
....
// inside the item listener...
selectedPosition = position;
showDialog(DIALOG_USER_PASSWORD);

/* HERE IS WHERE i NEED THE SELECTED ITEM 
mId IS THE OBJECT ASSOCIATED TO THE SELECTED POSITION */
// just use selectedPosition var

Any more elegant idea?

It seems that you use a normal ListView (not a checkbox one)... so, it's fine to do it this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文