开发者

Acess AdapterView from Dialog

开发者 https://www.devze.com 2023-01-30 06:13 出处:网络
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?

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,开发者_如何学C
                    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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消